Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixMenus.java
1 
16 package net.squiz.matrix.ui;
17 
18 import net.squiz.matrix.core.*;
19 import net.squiz.matrix.debug.*;
20 import net.squiz.matrix.assetmap.*;
21 import net.squiz.matrix.matrixtree.*;
22 
23 import javax.swing.*;
24 import javax.swing.tree.*;
25 import java.util.*;
26 import java.awt.*;
27 import javax.swing.event.*;
28 import java.awt.event.*;
29 import java.net.*;
30 
35 public class MatrixMenus implements MatrixConstants {
36 
37  private static JMenu addMenu;
38  private static ImageLoader loader = new ImageLoader();
39  public static final String DEFAULT_ASSET_ICON = "default_asset.png";
40 
41  // cannot instantiate
42  private MatrixMenus() {}
43 
57  public static JPopupMenu getPopupScreenMenu(MatrixTreeNode node) {
58  return getScreenMenu(node).getPopupMenu();
59  }
60 
76  public static JPopupMenu getPopupScreenMenu(
77  MatrixTreeNode node, ActionListener listener) {
78  return getScreenMenu(node, listener).getPopupMenu();
79  }
80 
94  public static JMenu getScreenMenu(MatrixTreeNode node) {
95  return getScreenMenu(node, getDefaultScreenMenuListener(node));
96  }
97 
113  public static JMenu getScreenMenu(MatrixTreeNode node, ActionListener listener) {
114  JMenu menu = new JMenu();
115  final Asset asset = node.getAsset();
116  AssetType type = asset.getType();
117  Iterator screens = type.getScreens();
118 
119  while (screens.hasNext()) {
120  AssetTypeScreen screen = (AssetTypeScreen) screens.next();
121  JMenuItem item = new JMenuItem(screen.getScreenName());
122  item.setFont(MatrixTreeBus.getActiveTree().getFontInUse());
123  item.addActionListener(listener);
124  item.setActionCommand(screen.getCodeName());
125  menu.add(item);
126  }
127  return menu;
128  }
129 
137  public static ActionListener getDefaultScreenMenuListener(final MatrixTreeNode node) {
138  ActionListener screenListener = new ActionListener() {
139  public void actionPerformed(ActionEvent evt) {
140  Asset asset = node.getAsset();
141  String command = evt.getActionCommand();
142 
143  String assetPath = MatrixToolkit.rawUrlEncode(node.getAssetPath(), true);
144  String linkPath = MatrixToolkit.rawUrlEncode(node.getLinkPath(), true);
145  String assetid = MatrixToolkit.rawUrlEncode(asset.getId(), true);
146 
147  String screenUrl = getScreenUrl(
148  assetid,
149  assetPath,
150  linkPath,
151  command
152  );
153  try {
154  AssetMap.getURL(screenUrl);
155  } catch (MalformedURLException mue) {
156  Object[] transArgs = {
157  mue.getMessage()
158  };
159  String message = Matrix.translate("asset_map_error_screen_url", transArgs);
160  GUIUtilities.error(message, Matrix.translate("asset_map_dialog_title_error"));
161  Log.log(message, MatrixMenus.class, mue);
162  }
163  }
164  };
165  return screenListener;
166  }
174  private static String getScreenUrl(
175  String assetid,
176  String assetPath,
177  String linkPath,
178  String screenName) {
179  String url =
180  Matrix.getProperty("parameter.url.baseurl") +
181  Matrix.getProperty("parameter.backendsuffix") +
182  "/?SQ_BACKEND_PAGE=main&backend_section=" +
183  "am&am_section=edit_asset&assetid=" + assetid +
184  "&sq_asset_path=" + assetPath + "&sq_link_path=" +
185  linkPath + "&asset_ei_screen=" + screenName;
186  return url;
187  }
188 
189  // }}}
190 
191  // {{{ Use Me Menu
192 
199  public static JPopupMenu getUseMeMenu(final MatrixTreeNode node) {
200  JPopupMenu menu = new JPopupMenu();
201 
202  ActionListener useMeListener = new ActionListener() {
203  public void actionPerformed(ActionEvent evt) {
204  Asset asset = node.getAsset();
205  String[] info = new String[] {
206  asset.getId(),
207  node.getName(),
208  node.getURL(),
209  node.getLinkid(),
210  asset.getType().toString(),
211  };
212  netscape.javascript.JSObject window = netscape.javascript.JSObject.getWindow(AssetMap.getApplet());
213  window.call("asset_finder_done", info);
214  MatrixTreeBus.stopAssetFinderMode();
215  }
216  };
217 
218  JMenuItem item = new JMenuItem(Matrix.translate("asset_map_menu_useme"));
219  item.addActionListener(useMeListener);
220  item.setFont(MatrixTreeBus.getActiveTree().getFontInUse());
221  menu.add(item);
222 
223  return menu;
224  }
225 
226  // }}}
227 
228  // {{{ Add Menu
229 
236  public static ActionListener getMatrixTreeAddMenuListener(final MatrixTree tree) {
237  ActionListener addMenuListener = new ActionListener() {
238  public void actionPerformed(ActionEvent evt) {
239  if (evt.getSource() instanceof JMenuItem) {
240  JMenuItem item = (JMenuItem) evt.getSource();
241 
242  tree.initiateAddMode(
243  new TreePath[] { new TreePath(MatrixMenus.getTypeCodeFromEvent(evt)) },
244  item.getLocation()
245  );
246  }
247  }
248  };
249  return addMenuListener;
250  }
251 
261  public static JPopupMenu getPopupAddMenu(ActionListener listener) {
262  JPopupMenu popupAddMenu = getAddMenu(listener).getPopupMenu();
263  popupAddMenu.addPopupMenuListener(loader);
264  return popupAddMenu;
265  }
266 
273  public static JMenu getAddMenu(ActionListener listener) {
274 
275  // if the menu is not null, then we have already created it
276  // so just return it
277  // if (menu != null)
278  // return menu;
279 
280  addMenu = new JMenu(Matrix.translate("asset_map_menu_add_new"));
281  addMenu.addMenuListener(loader);
282  Iterator it = AssetManager.getAssetTypes();
283 
284  while (it.hasNext()) {
285  AssetType type = (AssetType) it.next();
286 
287  // if this asset type is not createable by this user
288  // then we don't want it in the menu
289  if (!(type.isCreatable()))
290  continue;
291 
292  // get a list of menu paths for this asset type
293  // this may be null which indicates that there are no
294  // paths for this asset type (it exists in the root "Add" menu)
295 
296  String[] paths = type.getMenuPath();
297  MenuElement parentMenu = addMenu;
298 
299  if (paths.length > 0) {
300 
301  boolean found = false;
302  String path = null;
303  MenuElement[] elements = null;
304 
305  for (int i = 0; i < paths.length; i++) {
306  path = paths[i];
307  found = false;
308 
309  // get the current parent menu, and see if we
310  // already have this path
311  elements = parentMenu.getSubElements();
312 
313  if (elements.length > 0 && elements[0] instanceof JPopupMenu)
314  elements = elements[0].getSubElements();
315 
316  for (int j = 0; j < elements.length; j++) {
317  Component component = (Component) elements[j];
318  if (component instanceof JMenu) {
319  if (component.getAccessibleContext().getAccessibleName().equals(path)) {
320  // set the current parent menu to the menu that we just found
321  parentMenu = (JMenu) component;
322  found = true;
323  }
324  }
325  }//end for
326 
327  if (!found) {
328  // if we cant find it, create a new JMenu to
329  // list this asset type
330  JMenu newMenu = new JMenu(path);
331  newMenu.getAccessibleContext().setAccessibleName(path);
332  newMenu.addMenuListener(loader);
333 
334  addNewItem(
335  parentMenu,
336  newMenu,
337  getMenuIndex(path, parentMenu, newMenu.getClass()),
338  listener
339  );
340 
341  // if there are no more paths for this menu,
342  // then we want to add this asset type under the
343  // newly created menu
344 
345  if (i == paths.length - 1) {
346  addMenuItem(newMenu, type, listener);
347  }
348  } else {
349  // else we found the JMenu for this path
350  // so just add the new asset type under that menu
351  addMenuItem(parentMenu, type, listener);
352  }//end if not found
353  }//end for
354  } else {
355  // there are no paths for this asset type so just add it in
356  // the root "Add" menu
357  addMenuItem(addMenu, type, listener);
358  }//end if has paths
359  }//end while
360 
361  return addMenu;
362  }
363 
370  private static int getMenuIndex(String name, MenuElement parentMenu, Class cls){
371  int i = 0;
372  MenuElement[] components = parentMenu.getSubElements();
373  for (i = 0; i < components.length; i++) {
374 
375  // because the way that popups work, there may be some menus
376  // whose only child is a popupmenu, in that case, we want the children
377  // of the popup menu to be processed
378  if (components[i] instanceof JPopupMenu)
379  return getMenuIndex(name, components[i], cls);
380 
381  Component nextMenu = (Component) components[i];
382  String otherName = "";
383 
384  if (nextMenu.getClass().equals(JMenuItem.class)) {
385  String typeCode = nextMenu.getAccessibleContext().getAccessibleName();
386  otherName = AssetManager.getAssetType(typeCode).getName();
387  } else {
388  otherName = nextMenu.getAccessibleContext().getAccessibleName();
389  }
390  // if its just a single item, then we want it down the bottom
391  if (cls.equals(JMenuItem.class) && nextMenu.getClass().equals(JMenu.class))
392  continue;
393  if (name.compareToIgnoreCase(otherName) < 0)
394  return i;
395  }
396  return i;
397  }
398 
405  private static void addMenuItem(MenuElement parentMenu, AssetType type, ActionListener listener) {
406  JMenuItem newItem = new JMenuItem(type.getName());
407  // newItem.addActionListener(this);
408  newItem.getAccessibleContext().setAccessibleName(type.getTypeCode());
409  // add single com menu items down the bottom
410  int index = getMenuIndex(type.getName(),
411  parentMenu,
412  newItem.getClass()
413  );
414  addNewItem(parentMenu, newItem, index, listener);
415  }
416 
423  private static void addNewItem(
424  MenuElement parent,
425  JMenuItem item,
426  int index,
427  ActionListener listener) {
428  item.setFont(MatrixTreeBus.getActiveTree().getFontInUse());
429  if (parent instanceof JPopupMenu)
430  ((JPopupMenu) parent).add(item, index);
431  else
432  ((JMenu) parent).add(item, index);
433  item.addActionListener(listener);
434  }
435 
441  public static String getTypeCodeFromEvent(ActionEvent evt) {
442  JMenuItem item = (JMenuItem) evt.getSource();
443  return item.getAccessibleContext().getAccessibleName();
444  }
445 
451  static class ImageLoader implements MenuListener, PopupMenuListener {
452 
457  private void loadImages(MenuElement menu) {
458  final MenuElement[] elements = menu.getSubElements();
459  for (int i = 0; i < elements.length; i++) {
460  if (!(elements[i] instanceof JMenu)) {
461  String typeCode = ((Component) elements[i])
462  .getAccessibleContext().getAccessibleName();
463  final AssetType type = AssetManager.getAssetType(typeCode);
464 
465  // because the way that popups work, there may be some menus
466  // whose only child is a popupmenu, in that case, we want
467  // the children of the popup menu to be processed
468  if (elements[i] instanceof JPopupMenu) {
469  loadImages(elements[i]);
470  } else {
471  final JMenuItem nextItem = (JMenuItem) elements[i];
472  // Use an initial loadingIcon and fetch the actual icon
473  // in a thread
474  if (!type.isIconLoaded()) {
475  nextItem.setIcon(GUIUtilities.getAssetMapIcon(DEFAULT_ASSET_ICON));
476  MatrixSwingWorker worker = new MatrixSwingWorker() {
477  public Object construct() {
478  Icon icon = type.getIcon();
479  nextItem.setIcon(icon);
480  return null;
481  }
482  };
483  worker.start();
484  } else {
485  nextItem.setIcon(type.getIcon());
486  }
487  }
488  }//end if
489  }//end for
490  }
491 
496  public void menuSelected(MenuEvent evt) {
497  JMenu menu = (JMenu) evt.getSource();
498  loadImages(menu);
499  }
500 
505  public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
506  JPopupMenu menu = (JPopupMenu) evt.getSource();
507  loadImages(menu);
508  }
509 
510  public void menuCanceled(MenuEvent evt) {}
511  public void menuDeselected(MenuEvent evt) {}
512  public void popupMenuCanceled(PopupMenuEvent evt) {}
513  public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {}
514 
515  }//end class ImageLoader
516 }