Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
AssetMap.java
1 
16 package net.squiz.matrix.assetmap;
17 
18 import javax.swing.*;
19 import javax.swing.tree.*;
20 import net.squiz.matrix.ui.*;
21 import net.squiz.matrix.core.*;
22 import net.squiz.matrix.matrixtree.*;
23 import net.squiz.matrix.plaf.*;
24 import java.util.*;
25 import java.awt.event.*;
26 import java.awt.*;
27 import java.net.*;
28 import netscape.javascript.*;
29 import javax.swing.plaf.metal.*;
30 import javax.swing.border.*;
31 import net.squiz.matrix.debug.*;
32 import java.io.IOException;
33 import org.w3c.dom.*;
34 import java.security.AccessControlException;
35 
40 public class AssetMap extends JApplet implements InitialisationListener, KeyListener, ContainerListener {
41 
42  private BasicView view1;
43  private BasicView view2;
44  private MatrixTabbedPane pane;
45  protected static JSObject window;
46  public static AssetMap applet;
47  private javax.swing.Timer timer;
48  public static final int POLLING_TIME = 2000;
49 
53  public AssetMap() {
54  try {
55  UIManager.setLookAndFeel(new MatrixLookAndFeel());
56  } catch (UnsupportedLookAndFeelException ulnfe) {
57  ulnfe.printStackTrace();
58  }
59  applet = this;
60 
61  addKeyAndContainerListenerRecursively(this);
62  }
63 
64  // MM: find a better solution for doing this
65  // there is a way to get the root container (this)
66  public static AssetMap getApplet() {
67  return applet;
68  }
69 
74  public static void getURL(String url) throws MalformedURLException {
75  applet.getAppletContext().showDocument(new URL(url), "sq_main");
76  }
77 
84  public static void openWindow(String url, String title) {
85  if (window == null)
86  window = JSObject.getWindow(applet);
87  window.call("open_hipo", new Object[] { url } );
88  }
89 
90  public void initialisationComplete(InitialisationEvent evt) {}
91 
92 
98  public void init() {
99  window = JSObject.getWindow(this);
100  loadParameters();
102  getContentPane().add(createApplet());
103 
104  addKeyListener(this);
105  }
106 
116  public void start() {
117 
118  initAssetMap();
119  startPolling();
120  }
121 
125  private void javaVersionCheck() {
126 
127  boolean supportedVersion = false;
128 
129  String version = System.getProperty("java.version");
130  String[] supVersions = (Matrix.getProperty("parameter.java.supportedversion")).split("\\,");
131 
132  // compare versions
133  for (int i=0; i< supVersions.length; i++) {
134  if (version.startsWith(supVersions[i])) {
135  supportedVersion = true;
136  break;
137  }
138  }
139 
140  if (!supportedVersion) {
141  Object[] options = { Matrix.translate("ok"), Matrix.translate("cancel") };
142  Object[] transArgs = {
143  version,
144  Matrix.getProperty("parameter.java.supportedversion").replaceAll(",",", ")
145  };
146 
147  int ret = JOptionPane.showOptionDialog(null, Matrix.translate("asset_map_error_java_version", transArgs),
148  Matrix.translate("asset_map_error_java_version_title"),
149  JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
150 
151  if (ret == 0) {
152  // go to the SUN Java download page
153  try {
154  getAppletContext().showDocument(new URL(Matrix.getProperty("parameter.java.sunurl")), "_blank");
155  } catch (java.net.MalformedURLException exp) {
156  System.out.println(exp.getMessage());
157  }
158  }
159  }
160 
161  }
162 
167  protected void initAssetMap() {
168  // get a swing worker to call init in AssetManager
169  // when it returns set the root node to all trees
170  MatrixStatusBar.setStatus(Matrix.translate("asset_map_status_bar_init"));
171  MatrixSwingWorker worker = new MatrixSwingWorker() {
172  public Object construct() {
173  MatrixTreeNode root = null;
174  try {
175  root = AssetManager.init();
176  GUIUtilities.getAssetMapIcon(MatrixMenus.DEFAULT_ASSET_ICON);
177  } catch (IOException ioe) {
178  return ioe;
179  }
180  return root;
181  }
182 
183  public void finished() {
184  Object get = get();
185  // success
186  if (get instanceof MatrixTreeNode) {
187 
188  // Check if root folder asset is the actual root node for this user
189  // if not use the specified asset as the root node
190  String newRoot = Matrix.getProperty("parameter.rootlineage");
191  MatrixTreeModelBus.setRoot((MatrixTreeNode) get());
192 
193  if (newRoot.length() > 0) {
194  // root folder asset is not the root node
195  String[] info = newRoot.split("~");
196  String[] assetIds = info[0].split("\\|");
197  String[] sort_orders = info[1].split("\\|");
198 
199  // update tree root nodes
200  Iterator trees = MatrixTreeBus.getTrees();
201  while (trees.hasNext()) {
202  MatrixTree tree = (MatrixTree) trees.next();
203  // collapse the current root so we dont see its kids while switching root nodes
204  tree.collapsePath(tree.getPathToRoot((MatrixTreeNode)tree.getModel().getRoot()));
205  // find the specified asset/link and switch root node
206  tree.loadChildAssets(assetIds, sort_orders, false, true);
207 
208 
209  }
210  }
211 
212  try {
213  // if we have an initial lineage selected (i.e. from /_admin) then expand the tree
214  String initial_lineage = Matrix.getProperty("parameter.initialselection");
215  if (initial_lineage.length() > 0) {
216  String[] init_info = initial_lineage.split("~");
217  String[] init_assetIds = init_info[0].split("\\|");
218  String[] init_sort_orders = init_info[1].split("\\|");
219  Iterator trees = MatrixTreeBus.getTrees();
220  while (trees.hasNext()) {
221  MatrixTree tree = (MatrixTree) trees.next();
222  tree.loadChildAssets(init_assetIds, init_sort_orders, true, false);
223  }
224  }
225  } catch (Exception exp) {}
226  MatrixStatusBar.setStatusAndClear(Matrix.translate("asset_map_status_bar_success"), 1000);
227  // error
228  } else if (get instanceof IOException) {
229  IOException ioe = (IOException) get;
230  GUIUtilities.error(
231  AssetMap.this, ioe.getMessage(), Matrix.translate("asset_map_dialog_title_init_failed"));
232  MatrixStatusBar.setStatusAndClear(Matrix.translate("asset_map_status_bar_init_failed"), 1000);
233  Log.log("Could not initialise the asset map", AssetMap.class, ioe);
234  }
235  }
236  };
237  worker.start();
238  }
239 
246  protected void startPolling() {
247  // Polling timer
248  ActionListener taskPerformer = new ActionListener() {
249  public void actionPerformed(ActionEvent evt) {
250  if (window == null)
251  JSObject.getWindow(AssetMap.this);
252 
253  String assetidsStr = (String) window.getMember("SQ_REFRESH_ASSETIDS");
254  // if the string isn't empty, we have some work to do.
255  // split the string for the asset ids and get the refresh worker
256  // to start the refresh operation
257  if (assetidsStr != null && !assetidsStr.equals("")) {
258  String[] assetids = assetidsStr.split("\\|");
259  AssetRefreshWorker worker = new AssetRefreshWorker(assetids, false) {
260  // return a custom message for the wait message
261  protected String getStatusBarWaitMessage() {
262  return Matrix.translate("asset_map_status_bar_auto_refresh");
263  }
264  };
265  worker.start();
266  // clear the assets that we have refreshed
267  window.eval("SQ_REFRESH_ASSETIDS = '';");
268  }
269  }
270  };
271  timer = new javax.swing.Timer(POLLING_TIME, taskPerformer);
272  timer.start();
273  }
274 
284  public void loadParameters() {
285  // get the list of parameters availble to the asset map
286  // and store them in the matrix property set.
287  String paramStr = getParameter("parameter.params");
288  String[] params = paramStr.split(",");
289 
290  for (int i = 0; i < params.length; i++)
291  Matrix.setProperty(params[i], getParameter(params[i]));
292  }
293 
298  public void loadTranslations() {
299  Document response = null;
300 
301  try {
302  response = Matrix.doRequest("<command action=\"get translations\" />");
303  } catch (IOException ioe) {
304  ioe.printStackTrace();
305  }
306 
307  Element rootElement = response.getDocumentElement();
308 
309  // Set the Java VM locale to the Matrix locale if available
310  String currentLocale = rootElement.getAttribute("locale");
311  Locale availLocales[] = Locale.getAvailableLocales();
312  for (int j = 0; j < availLocales.length; j++) {
313  if (availLocales[j].toString().equals(currentLocale)) {
314  try {
315  Locale.setDefault(availLocales[j]);
316  } catch (AccessControlException ace) {
317  Log.log("Error setting locale, invalid permisions", AssetMap.class, ace);
318  }
319  }
320  }
321 
322  // Grab the .properties file from the XML CDATA
323  NodeList children = rootElement.getChildNodes();
324  for (int i = 0; i < children.getLength(); i++) {
325  if (!(children.item(i) instanceof CDATASection))
326  continue;
327 
328  CDATASection cdata = (CDATASection) children.item(i);
329  Matrix.setTranslationFile(cdata.getData());
330  }
331  }
332 
338  public void stop() {
339  timer.stop();
340  timer = null;
341  }
342 
347  protected JComponent createApplet() {
348  getContentPane().setBackground(new Color(0x342939));
349 
350  pane = new MatrixTabbedPane(JTabbedPane.LEFT);
351  view1 = new BasicView();
352  view2 = new BasicView();
353 
354  pane.addView(Matrix.translate("asset_map_tree1_name"), view1);
355  pane.addView(Matrix.translate("asset_map_tree2_name"), view2);
356 
357  return pane;
358  }
359 
376  public void jsToJavaCall(String type, String command, String params) {
377  if (type.equals("asset_finder")) {
378  processAssetFinder(command, params);
379  } else if (type.equals("asset_locator")) {
380  processAssetLocator(params);
381  }
382  }
383 
384  public void processAssetLocator(String params) {
385  // we need to create 2 arrays
386  String[] info = params.split("~");
387  String[] assetIds = info[0].split("\\|");
388  String[] sort_orders = info[1].split("\\|");
389  MatrixTreeBus.startAssetLocator(assetIds, sort_orders);
390  }
391 
392  private void processAssetFinder(String command, String params) {
393  if (command.equals("assetFinderStarted")) {
394  String[] assetTypes = params.split("~");
395  MatrixTreeBus.startAssetFinderMode(assetTypes);
396  } else if (command.equals("assetFinderStopped")) {
397  MatrixTreeBus.stopAssetFinderMode();
398  }
399  }
400 
401  public void keyPressed(KeyEvent e) {
402  if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
403  Iterator trees = MatrixTreeBus.getTrees();
404 
405  while (trees.hasNext()) {
406  MatrixTree tree = (MatrixTree) trees.next();
407  tree.stopCueMode();
408  }
409  }
410  }
411 
412  public void keyTyped(KeyEvent e) {}
413  public void keyReleased(KeyEvent e) {}
414 
415  public void componentAdded(ContainerEvent e) {
416  addKeyAndContainerListenerRecursively(e.getChild());
417  }
418 
419  public void componentRemoved(ContainerEvent e) {
420  removeKeyAndContainerListenerRecursively(e.getChild());
421  }
422 
423  public void addKeyAndContainerListenerRecursively(Component c) {
424  c.addKeyListener(this);
425 
426  if (c instanceof Container) {
427  Container cont = (Container)c;
428  cont.addContainerListener(this);
429 
430  Component[] children = cont.getComponents();
431  for (int i = 0; i < children.length; i++) {
432  addKeyAndContainerListenerRecursively(children[i]);
433  }
434  }
435  }
436 
437  public void removeKeyAndContainerListenerRecursively(Component c) {
438  c.removeKeyListener(this);
439 
440  if (c instanceof Container) {
441  Container cont = (Container)c;
442  cont.removeContainerListener(this);
443 
444  Component[] children = cont.getComponents();
445  for (int i = 0; i < children.length; i++) {
446  removeKeyAndContainerListenerRecursively(children[i]);
447  }
448  }
449  }
450 }