package com.ericsson.tic.vi; import javax.swing.JPanel; import javax.swing.JSplitPane; import java.awt.BorderLayout; import java.awt.Dimension; import java.util.ListIterator; /** * A panel which can be split multiple times. * * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class SplitViewPanel extends JPanel { /** A splitpane. */ public JSplitPane splitPane; /** A ViewPanel. */ private ViewPanel view; /** Leaf panels. */ public SplitViewPanel panel0, panel1; /** The parent of this split view. null if this is the root. */ public SplitViewPanel parent; /** True if this panel is split. */ public boolean isSplit; /** * Creates the main view panel which can be split into subpanels. * * @param parent The parent of the splitViewPanel. null if this is the root. * @param view The initial ViewPanel */ public SplitViewPanel(SplitViewPanel parent, ViewPanel view) { this.parent = parent; this.isSplit = false; if (view == null) { this.view = new ViewPanel(VI.requestViewID() , this); } else { this.view = view; this.view.splitParent = this; } if (parent != null) { this.view.closeMenuItem.setEnabled(true); } this.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(200, 200)); this.add(this.view, BorderLayout.CENTER); } /** * Splits the view vertically. */ public void splitVertical() { panel0 = new SplitViewPanel(this, this.view); panel1 = new SplitViewPanel(this, null); splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel0, panel1); this.view = null; this.removeAll(); this.setLayout(new BorderLayout()); this.isSplit = true; this.add(splitPane, BorderLayout.CENTER); this.revalidate(); ListIterator iter = VI.windowList.listIterator(); GUI gui = null; while (iter.hasNext()) { gui = (GUI)iter.next(); DataListPanel dp = (DataListPanel)gui.list.viewsCard; dp.updateViewList(); } } /** * Splits the view horizontally. */ public void splitHorizontal() { panel0 = new SplitViewPanel(this, view); panel1 = new SplitViewPanel(this, null); splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel0, panel1); this.view = null; this.removeAll(); this.setLayout(new BorderLayout()); this.isSplit = true; this.add(splitPane, BorderLayout.CENTER); this.revalidate(); ListIterator iter = VI.windowList.listIterator(); GUI gui = null; while (iter.hasNext()) { gui = (GUI)iter.next(); DataListPanel dp = (DataListPanel)gui.list.viewsCard; dp.updateViewList(); } } /** * Closes one of the views. This method is currently bugged. */ public void closeView() { // todo: make this work! // this method is currently bugged and will cause one of the views to // crash or makes it impossible to create new views // problem most likely lies in some problem with the tree structure // which keeps track of the panes. if (parent != null) { if (this.equals(parent.panel0)) { ListIterator iter = VI.viewList.listIterator(); ViewPanel vp = null; while (iter.hasNext()) { vp = (ViewPanel)iter.next(); if (vp.id == parent.panel0.view.id) { iter.remove(); } } if (parent.panel1.isSplit) { parent.splitPane = parent.panel1.splitPane; SplitViewPanel t0 = parent.panel1.panel0; SplitViewPanel t1 = parent.panel1.panel1; parent.panel0 = t0; parent.panel1 = t1; parent.view = null; parent.removeAll(); parent.add(parent.splitPane, BorderLayout.CENTER); } else { parent.splitPane = null; parent.view = parent.panel1.view; parent.panel0 = null; parent.panel1 = null; //this.isSplit = false; parent.isSplit = false; parent.removeAll(); parent.add(parent.view, BorderLayout.CENTER); if (parent.parent == null) { parent.view.closeMenuItem.setEnabled(false); } } } else { ListIterator iter = VI.viewList.listIterator(); ViewPanel vp = null; while (iter.hasNext()) { vp = (ViewPanel)iter.next(); if (vp.id == parent.panel1.view.id) { iter.remove(); } } if (parent.panel0.isSplit) { parent.splitPane = parent.panel0.splitPane; SplitViewPanel t0 = parent.panel0.panel0; SplitViewPanel t1 = parent.panel0.panel1; parent.panel0 = t0; parent.panel1 = t1; parent.view = null; parent.removeAll(); parent.add(parent.splitPane, BorderLayout.CENTER); } else { parent.splitPane = null; parent.view = parent.panel0.view; parent.panel0 = null; parent.panel1 = null; //this.isSplit = false; parent.isSplit = false; parent.removeAll(); parent.add(parent.view, BorderLayout.CENTER); if (parent.parent == null) { parent.view.closeMenuItem.setEnabled(false); } } } parent.revalidate(); } } }