1 package jspecview.dialog; 2 3 import java.util.Hashtable; 4 import java.util.Map; 5 6 import javajs.util.PT; 7 8 import jspecview.api.JSVPanel; 9 import jspecview.api.PlatformDialog; 10 import jspecview.common.Spectrum; 11 import jspecview.common.JSVFileManager; 12 import jspecview.common.JSViewer; 13 import jspecview.source.JDXSource; 14 15 /** 16 * Dialogs include Integration, PeakListing, Views, OverlayLegend, and Measurements 17 * These dialogs have been generalized for platform independence.' 18 * 19 * This manager is subclassed as AwtDialogManager and JsDialogManager, which apply their 20 * own interpretation of how to create the dialog and get its event callbacks. For any 21 * one session, there will be only one DialogManager, created in JSViewer. 22 * 23 * AwtDialogManager will create instances of AwtDialog extends javax.swing.JDialog; 24 * JsDialogManager will create instances of JsDialog extends jspecview.awtj2d.swing.JDialog. 25 * 26 * @author hansonr 27 * 28 */ 29 abstract public class DialogManager { 30 31 protected JSViewer vwr; 32 private Map<Object, String> htSelectors; 33 protected Map<String, JSVDialog> htDialogs; 34 private Map<String, Object> options; 35 set(JSViewer viewer)36 public DialogManager set(JSViewer viewer) { 37 this.vwr = viewer; 38 htSelectors = new Hashtable<Object, String>(); 39 htDialogs = new Hashtable<String, JSVDialog>(); 40 return this; 41 } 42 43 public final static int PLAIN_MESSAGE = -1; // JOptionPane.PLAIN_MESSAGE 44 public final static int ERROR_MESSAGE = 0; // JOptionPane.ERROR_MESSAGE 45 public final static int INFORMATION_MESSAGE = 1; // JOptionPane.INFORMATION_MESSAGE 46 public final static int WARNING_MESSAGE = 2; // JOptionPane.WARNING_MESSAGE 47 public final static int QUESTION_MESSAGE = 3; // JOptionPane.QUESTION_MESSAGE 48 getDialog(JSVDialog jsvDialog)49 abstract public PlatformDialog getDialog(JSVDialog jsvDialog); 50 getDialogInput(Object parentComponent, String phrase, String title, int msgType, Object icon, Object[] objects, String defaultStr)51 abstract public String getDialogInput(Object parentComponent, String phrase, 52 String title, int msgType, Object icon, Object[] objects, 53 String defaultStr); 54 getLocationOnScreen(Object component)55 abstract public int[] getLocationOnScreen(Object component); 56 getOptionFromDialog(Object frame, String[] items, JSVPanel jsvp, String dialogName, String labelName)57 abstract public int getOptionFromDialog(Object frame, String[] items, JSVPanel jsvp, 58 String dialogName, String labelName); 59 showMessageDialog(Object parentComponent, String msg, String title, int msgType)60 abstract public void showMessageDialog(Object parentComponent, String msg, String title, int msgType); 61 showProperties(Object frame, Spectrum spectrum)62 abstract public void showProperties(Object frame, Spectrum spectrum); 63 showMessage(Object frame, String text, String title)64 abstract public void showMessage(Object frame, String text, String title); 65 66 67 /** 68 * register the JSV dialog with a unique key to be used as an ID in callbacks 69 * optionKeys ending with "!" are one-of-a-kind, such as "views" 70 * 71 * @param jsvDialog 72 * @return id 73 */ registerDialog(JSVDialog jsvDialog)74 protected String registerDialog(JSVDialog jsvDialog) { 75 String id = jsvDialog.optionKey; 76 if (!id.endsWith("!")) 77 id += " " + ("" + Math.random()).substring(3); 78 if (htDialogs.containsKey(id)) 79 htDialogs.get(id).dispose(); 80 htDialogs.put(id, jsvDialog); 81 return id; 82 } 83 registerSelector(String selectorName, Object columnSelector)84 public void registerSelector(String selectorName, Object columnSelector) { 85 htSelectors.put(columnSelector, selectorName); 86 } 87 getSelectorName(Object selector)88 protected String getSelectorName(Object selector) { 89 return htSelectors.get(selector); 90 } 91 showSourceErrors(Object frame, JDXSource currentSource)92 public void showSourceErrors(Object frame, JDXSource currentSource) { 93 if (currentSource == null) { 94 showMessageDialog(frame, 95 "Please Select a Spectrum.", "Select Spectrum", WARNING_MESSAGE); 96 return; 97 } 98 String errorLog = currentSource.getErrorLog(); 99 if (errorLog != null && errorLog.length() > 0) 100 showMessage(frame, errorLog, fixTitle(currentSource.getFilePath())); 101 else 102 showMessageDialog(frame, "No errors found.", 103 "Error Log", INFORMATION_MESSAGE); 104 } 105 showSource(Object frame, Spectrum spec)106 public void showSource(Object frame, Spectrum spec) { 107 String filePath = spec.getFilePath(); 108 if (filePath == null) { 109 showMessageDialog(frame, "Please Select a Spectrum", "Select Spectrum", 110 WARNING_MESSAGE); 111 return; 112 } 113 if (filePath == "[inline]") { 114 showMessage(null, spec.getInlineData(), "Inline data"); 115 return; 116 } 117 try { 118 String s = JSVFileManager.getFileAsString(filePath); 119 if (vwr.isJS) 120 s = PT.rep(s, "<", "<"); 121 showMessage(null, s, fixTitle(filePath)); 122 } catch (Exception ex) { 123 showMessageDialog(frame, "File Not Found", "SHOWSOURCE", ERROR_MESSAGE); 124 } 125 } 126 127 /** 128 * processing click event from platform DialogManager 129 * 130 * @param eventId dialogId/buttonId starting with "btn", "chk", "cmb", or "txt" 131 */ 132 processClick(String eventId)133 protected void processClick(String eventId) { 134 int pt = eventId.lastIndexOf("/"); 135 String id = eventId.substring(pt + 1); 136 String dialog = eventId.substring(0, pt); 137 dialogCallback(dialog, id, null); 138 } 139 140 /** 141 * processing table cell click event from platform DialogManager; takes two 142 * hits in AWT -- one a row, the other a column 143 * 144 * @param eventId 145 * dialogId/[ROW|COL] or just dialogId 146 * @param index1 147 * row if just dialogId or (row or col if AWT) 148 * @param index2 149 * column if just dialogId or -1 if AWT 150 * @param adjusting 151 */ processTableEvent(String eventId, int index1, int index2, boolean adjusting)152 protected void processTableEvent(String eventId, int index1, int index2, 153 boolean adjusting) { 154 int pt = eventId.lastIndexOf("/"); 155 String dialog = eventId.substring(0, pt); 156 String selector = eventId.substring(pt + 1); 157 String msg = "&selector=" + selector + "&index=" + index1 158 + (index2 < 0 ? "&adjusting=" + adjusting : "&index2=" + index2); 159 dialogCallback(dialog, "tableSelect", msg); 160 } 161 162 /** 163 * processing window closing event from platform DialogManager 164 * 165 * @param dialogId 166 */ processWindowClosing(String dialogId)167 protected void processWindowClosing(String dialogId) { 168 dialogCallback(dialogId, "windowClosing", null); 169 htDialogs.remove(dialogId); 170 } 171 172 /** 173 * Send the callback to the appropriate dialog 174 * 175 * @param dialogId 176 * @param id 177 * @param msg 178 */ dialogCallback(String dialogId, String id, String msg)179 private void dialogCallback(String dialogId, String id, String msg) { 180 JSVDialog jsvDialog = htDialogs.get(dialogId); 181 if (jsvDialog != null) 182 jsvDialog.callback(id, msg); 183 } 184 185 /** 186 * persistent options such as units 187 * 188 * @return options 189 */ getDialogOptions()190 Map<String, Object> getDialogOptions() { 191 if (options == null) 192 options = new Hashtable<String, Object>(); 193 return options; 194 } 195 fixTitle(String title)196 public static String fixTitle(String title) { 197 return (title.length() > 50 ? title.substring(0, 50) + "..." : title); 198 } 199 200 } 201