Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
AssetMapMenuPanel.java
1 
16 package net.squiz.matrix.assetmap;
17 
18 import net.squiz.matrix.matrixtree.*;
19 import net.squiz.matrix.debug.*;
20 import net.squiz.matrix.core.*;
21 import net.squiz.matrix.ui.*;
22 import javax.swing.*;
23 import javax.swing.tree.*;
24 import java.awt.event.*;
25 import javax.swing.event.*;
26 import java.awt.*;
27 import java.io.IOException;
28 import java.util.*;
29 
30 
36 public class AssetMapMenuPanel extends JPanel {
37 
38  private JMenuBar menuBar;
39  private MatrixTree tree;
40  private JToolBar toolBar;
41 
42  public static final Color BG_COLOUR = new Color(0xF5F5F5);
43  public static final int ICON_GAP = 1;
44 
49  public AssetMapMenuPanel(MatrixTree tree, boolean includeCreateButton) {
50  this.tree = tree;
51 
52  setLayout(new BorderLayout());
53 
54  JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
55  JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
56 
57  rightPanel.add(createRefreshAssetsButton());
58  rightPanel.add(createRestoreRootButton());
59  rightPanel.add(createCollapseAllButton());
60  rightPanel.add(createPaintStatusesButton());
61 
62  if (includeCreateButton) {
63  leftPanel.add(createAddMenuButton());
64  add(leftPanel, BorderLayout.WEST);
65  }
66 
67  add(rightPanel, BorderLayout.EAST);
68 
69  leftPanel.setBackground(BG_COLOUR);
70  rightPanel.setBackground(BG_COLOUR);
71  setBackground(BG_COLOUR);
72  }
73 
74  public Dimension getPreferredSize() {
75  return new Dimension(300, 25);
76  }
77 
78  public Dimension getMinimumSize() {
79  return new Dimension(300, 25);
80  }
81 
82  public Dimension getMaximumSize() {
83  return new Dimension(300, 25);
84  }
85 
99  private JButton createButton(
100  String iconName,
101  ActionListener listener,
102  String toolTipText) {
103 
104  Icon icon = GUIUtilities.getAssetMapIcon(iconName + "_off.png");
105  Icon pressedIcon = GUIUtilities.getAssetMapIcon(iconName + "_on.png");
106 
107  JButton button = new JButton(icon);
108  button.setBackground(BG_COLOUR);
109  button.setBorderPainted(false);
110 
111  button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
112 
113  button.setPressedIcon(pressedIcon);
114  button.addActionListener(listener);
115  button.setToolTipText(toolTipText);
116 
117  return button;
118  }
119 
126  private JButton createRefreshAssetsButton() {
127 
128  ActionListener refreshListener = new ActionListener() {
129  public void actionPerformed(ActionEvent e) {
130  AssetRefreshWorker worker = new AssetRefreshWorker(true);
131  worker.start();
132  }
133  };
134  JButton refreshButton
135  = createButton("refresh", refreshListener, Matrix.translate("asset_map_tooltip_refresh_all"));
136 
137  return refreshButton;
138  }
139 
145  private JButton createRestoreRootButton() {
146 
147  ActionListener restoreListener = new ActionListener() {
148  public void actionPerformed(ActionEvent e) {
149  String newRoot = Matrix.getProperty("parameter.rootlineage");
150  // user can switch between the two root nodes
151  if ((newRoot.length() > 0) && (tree.getModel().getRoot() == AssetManager.getRootFolderNode())) {
152  // root folder is not the actual root
153  String[] info = newRoot.split("~");
154  String[] assetIds = info[0].split("\\|");
155  String[] sort_orders = info[1].split("\\|");
156  tree.collapsePath(tree.getPathToRoot((MatrixTreeNode)tree.getModel().getRoot()));
157  tree.loadChildAssets(assetIds, sort_orders, false, true);
158  } else {
159  tree.setRootVisible(false);
160  ((DefaultTreeModel) tree.getModel()).setRoot(AssetManager.getRootFolderNode());
161  }
162  }
163  };
164  JButton restoreButton
165  = createButton("teleport", restoreListener, Matrix.translate("asset_map_tooltip_restore_root"));
166 
167  return restoreButton;
168  }
169 
176  private JButton createCollapseAllButton() {
177 
178  ActionListener collapseListener = new ActionListener() {
179  public void actionPerformed(ActionEvent e) {
180  ((DefaultTreeModel) tree.getModel()).reload();
181  tree.repaint();
182  }
183  };
184  JButton collapseButton
185  = createButton("collapse", collapseListener, Matrix.translate("asset_map_tooltip_collapse_all"));
186 
187  return collapseButton;
188  }
189 
197  private JButton createPaintStatusesButton() {
198 
199  ActionListener statusListener = new ActionListener() {
200  public void actionPerformed(ActionEvent evt) {
201  ((MatrixTreeCellRenderer) tree.getCellRenderer()).flipSelection();
202  tree.repaint();
203  }
204  };
205  JButton paintStatusButton
206  = createButton("status", statusListener, Matrix.translate("asset_map_tooltip_toggle_status"));
207 
208  return paintStatusButton;
209  }
210 
215  private ButtonMenu createAddMenuButton() {
216 
217  Icon icon = GUIUtilities.getAssetMapIcon("add_off.png");
218  Icon pressedIcon = GUIUtilities.getAssetMapIcon("add_on.png");
219 
220  final ButtonMenu button = new ButtonMenu(icon, pressedIcon);
221 
222  // we need to do this because the asset map may not have made a request
223  // to matrix yet, so the add menu elements might not yet be known
224  ActionListener bListener = new ActionListener() {
225  private JPopupMenu addMenu;
226 
227  public void actionPerformed(ActionEvent evt) {
228  if (addMenu == null) {
229  ActionListener listener = MatrixMenus.getMatrixTreeAddMenuListener(tree);
230  addMenu = MatrixMenus.getPopupAddMenu(listener);
231  button.setPopupMenu(addMenu);
232  }
233  }
234  };
235 
236  button.addActionListener(bListener);
237  button.setPressedIcon(pressedIcon);
238  button.setBackground(BG_COLOUR);
239  button.setBorderPainted(false);
240 
241  button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
242  button.setToolTipText(Matrix.translate("asset_map_tooltip_add_asset"));
243 
244  return button;
245  }
246 }