Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixTabbedPane.java
1 
16 package net.squiz.matrix.assetmap;
17 
18 import net.squiz.matrix.ui.*;
19 import net.squiz.matrix.core.*;
20 import javax.swing.tree.TreePath;
21 import net.squiz.matrix.matrixtree.*;
22 
23 import javax.swing.*;
24 import javax.swing.event.*;
25 
26 import java.awt.event.*;
27 import java.awt.*;
28 import java.awt.dnd.*;
29 
30 public class MatrixTabbedPane extends VerticalTabbedPane {
31 
32  private javax.swing.Timer tabChangerTimer;
33  public final Redocker REDOCK_HANDLER = new Redocker();
34  private boolean allUndocked = false;
35 
36  public MatrixTabbedPane(int alignment) {
37  super(alignment);
38 
39  DropHandler dropHandler = new DropHandler();
40  DropTarget dropTarget = new DropTarget(this, dropHandler);
41 
42  MouseListener listener = new MouseAdapter() {
43  public void mouseClicked(MouseEvent evt) {
44  if (evt.getClickCount() == 2) {
45  JTabbedPane source = (JTabbedPane) evt.getSource();
46  View view = (View) source.getSelectedComponent();
47  if (view != null) {
48  // dont do anything if the click is not within the
49  // bounds of a tab
50  if (indexAtLocation(evt.getX(), evt.getY()) == -1)
51  return;
52 
53  JPanel panel = new JPanel();
54  panel.setBackground(Color.WHITE);
55  JLabel tabsUndocked = new JLabel("", GUIUtilities.getAssetMapIcon("matrix_logo.png"), CENTER);
56  tabsUndocked.setVerticalTextPosition(JLabel.CENTER);
57  tabsUndocked.setHorizontalTextPosition(JLabel.CENTER);
58  tabsUndocked.setVerticalAlignment(JLabel.CENTER);
59  tabsUndocked.setHorizontalAlignment(JLabel.CENTER);
60  tabsUndocked.setBackground(Color.WHITE);
61 
62  int fillerWidth = getWidth();
63  int fillerHeight = ( getHeight() / 3 ) ;
64  Dimension filler = new Dimension(fillerWidth,fillerHeight);
65 
66  panel.add(new Box.Filler(filler,filler,filler), BorderLayout.NORTH);
67  panel.add(tabsUndocked, BorderLayout.CENTER);
68  source.setComponentAt(source.getSelectedIndex(), panel);
69 
70  UndockedView udView = new UndockedView(view, source.getSelectedIndex());
71  AssetMap.applet.addKeyAndContainerListenerRecursively(udView);
72 
73  udView.setSize(300, 600);
74  GUIUtilities.showInScreenCenter(udView);
75  udView.toFront();
76  udView.setExtendedState(Frame.NORMAL);
77  udView.addWindowListener(REDOCK_HANDLER);
78  source.setEnabledAt(source.getSelectedIndex(), false);
79 
80  for (int i = 0; i < source.getTabCount(); i++) {
81  if (i != source.getSelectedIndex() && source.isEnabledAt(i)) {
82  source.setSelectedIndex(i);
83  allUndocked = false;
84  break;
85  }
86 
87  if (i == source.getTabCount() - 1) {
88  source.setSelectedIndex(-1);
89  allUndocked = true;
90  }
91  }
92  }
93  }
94  }
95  };
96 
97  addMouseListener(listener);
98  addChangeListener(new CueTransferHandler());
99  }
100 
101  public void addView(String name, View view) {
102  view.setName(name);
103  addTab(name, GUIUtilities.getAssetMapIcon("tree.png"), view.getViewComponent());
104  }
105 
106  public boolean isAllUndocked() {
107  return allUndocked ? true : false;
108  }
109 
110 
111  class Redocker extends WindowAdapter {
112  public void windowClosing(WindowEvent evt) {
113  UndockedView view = (UndockedView) evt.getComponent();
114  JComponent basicView = view.getViewComponent();
115 
116  JSplitPane splitPane = ((BasicView)basicView).getSplitPane();
117  splitPane.setDividerLocation(Integer.MAX_VALUE);
118 
119  for (int i = 0; i < getTabCount(); i++) {
120  // make the other disabled tab a basic view
121  // so that tabbedpane does not have problem to paint
122  if (isEnabledAt(i) == false && i != view.getIndex()) {
123  BasicView newView = new BasicView();
124  setComponentAt(i, newView);
125  }
126  }
127 
128  setComponentAt(view.getIndex(), basicView);
129  setEnabledAt(view.getIndex(), true);
130  setSelectedIndex(view.getIndex());
131  allUndocked = false;
132  }
133  }
134 
135  protected class CueTransferHandler implements ChangeListener {
136 
137  public void stateChanged(ChangeEvent evt) {
138 
139  try {
140  JTabbedPane pane = (JTabbedPane) evt.getSource();
141  BasicView view = (BasicView) pane.getComponentAt(pane.getSelectedIndex());
142  MatrixTree currentTree = view.getTree();
143 
144  for (int i = 0; i < pane.getTabCount(); i++) {
145  // it might be a JPanel so we need to check to see that its a BasicView
146  if (!(pane.getComponentAt(i) instanceof BasicView))
147  continue;
148  MatrixTree tree = ( (BasicView) ( pane.getComponentAt(i) ) ).getTree();
149  TreePath[] path = new TreePath[1];
150  path[0] = tree.getCuePath();
151  if (path[0] != null) {
152  currentTree.startCueMode(path);
153  tree.stopCueMode();
154  break;
155  }
156  }
157  } catch (ArrayIndexOutOfBoundsException ex) {}
158  }
159  }
160 
161  protected class DropHandler implements DropTargetListener {
162 
163  private javax.swing.Timer tabChangerTimer;
164  private Point lastMousePt;
165 
166  public DropHandler() {
167  ActionListener timerListener = new ActionListener() {
168  public void actionPerformed(ActionEvent evt) {
169  setSelectedIndex(indexAtLocation(lastMousePt.x, lastMousePt.y));
170  }
171  };
172  tabChangerTimer = new javax.swing.Timer(1000, timerListener);
173  tabChangerTimer.setRepeats(false);
174  }
175 
176  public void dragOver(DropTargetDragEvent dtde) {
177  lastMousePt = dtde.getLocation();
178  if (indexAtLocation(lastMousePt.x, lastMousePt.y) != -1) {
179  if (!tabChangerTimer.isRunning()) {
180  tabChangerTimer.start();
181  }
182  } else {
183  if (tabChangerTimer.isRunning())
184  tabChangerTimer.stop();
185  }
186  }
187 
188  public void dragEnter(DropTargetDragEvent dtde) {}
189  public void dragExit(DropTargetEvent dte) {}
190  public void drop(DropTargetDropEvent dtde) {}
191  public void dropActionChanged(DropTargetDragEvent dtde) {}
192 
193  }//end class DrapHandler
194 }