001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Jan 16, 2003 005 * Time: 8:39:52 PM 006 */ 007 008 package EVolve.util.predefinedutils; 009 010 import java.awt.*; 011 import java.awt.event.*; 012 import java.io.*; 013 import java.util.*; 014 import javax.swing.*; 015 import EVolve.Scene; 016 import EVolve.exceptions.*; 017 import EVolve.util.settings.SceneSetting; 018 import EVolve.util.xmlutils.*; 019 import EVolve.util.xmlutils.datastructures.SerializedData; 020 import EVolve.util.xmlutils.datastructures.SerializedVisualization; 021 import EVolve.util.xmlutils.datastructures.XMLWriteOrder; 022 import EVolve.util.xmlutils.datastructures.SerializedSelection; 023 import EVolve.visualization.Visualization; 024 025 public class VisualizationSerializer { 026 private static VisualizationSerializer instance = null; 027 private JDialog dialog; 028 private JCheckBox checkSelection; 029 private JTextField textConfigureName; 030 private JTextField textConfigureFile; 031 private JButton buttonConfigure; 032 private JButton buttonSave, buttonCancel; 033 034 private VisualizationSerializer() { 035 dialog = null; 036 } 037 038 public static VisualizationSerializer v() { 039 if (instance == null) instance = new VisualizationSerializer(); 040 return instance; 041 } 042 043 public void showSavingDialog() { 044 if (dialog == null) { 045 createDialog(); 046 } else { 047 if (Scene.getFilter().getSelection().length == 0) { 048 checkSelection.setSelected(false); 049 } 050 dialog.setVisible(true); 051 } 052 } 053 054 private void createDialog() { 055 Box boxMain = new Box(BoxLayout.Y_AXIS); 056 057 dialog = new JDialog(Scene.getFrame(), "Setting Saving...", true); 058 dialog.setBounds(new Rectangle(500,250)); 059 060 061 boxMain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 062 "Save configuration and selections")); 063 dialog.getContentPane().add(boxMain,BorderLayout.CENTER); 064 065 //*********************************** 066 Box boxConfigureName = new Box(BoxLayout.X_AXIS); 067 boxConfigureName.add(new JLabel("Configuration name:")); 068 textConfigureName = new JTextField(12); 069 boxConfigureName.add(Box.createHorizontalStrut(25)); 070 boxConfigureName.add(textConfigureName); 071 072 Box boxConfig = new Box(BoxLayout.X_AXIS); 073 boxConfig.add(new JLabel("Choose configure file:")); 074 textConfigureFile = new JTextField(12); 075 boxConfig.add(Box.createHorizontalStrut(20)); 076 boxConfig.add(textConfigureFile,BorderLayout.CENTER); 077 buttonConfigure = new JButton("..."); 078 buttonConfigure.addActionListener(new ActionListener(){ 079 public void actionPerformed(ActionEvent e) { 080 JFileChooser fc = selectFile(); 081 if (fc == null) return; 082 textConfigureFile.setText(fc.getSelectedFile().getPath()); 083 } 084 }); 085 boxConfig.add(buttonConfigure,BorderLayout.EAST); 086 087 //***************************** 088 Box boxChk = new Box(BoxLayout.X_AXIS); 089 checkSelection = new JCheckBox("Saving Filters & colors also"); 090 checkSelection.setMnemonic(KeyEvent.VK_F); 091 checkSelection.addActionListener(new ActionListener() { 092 public void actionPerformed(ActionEvent e) { 093 if (Scene.getFilter().getSelection().length == 0) { 094 Scene.showErrorMessage("No active selection exists!"); 095 checkSelection.setSelected(false); 096 } 097 } 098 }); 099 boxChk.add(checkSelection); 100 JLabel note = new JLabel("(If checked, the configuration file can ONLY be applied to " + 101 " the current data source.)"); 102 note.setFont(new Font("Dialog",Font.ITALIC|Font.BOLD,9)); 103 boxChk.add(note); 104 105 //*********************************** 106 Box boxSaveCancel = new Box(BoxLayout.X_AXIS); 107 buttonSave = new JButton("Save"); 108 buttonSave.addActionListener(new ActionListener() { 109 public void actionPerformed(ActionEvent e) { 110 if (!validate()) return; 111 dialogSave(); 112 } 113 }); 114 115 buttonCancel = new JButton("Cancel"); 116 buttonCancel.addActionListener(new ActionListener() { 117 public void actionPerformed(ActionEvent e) { 118 dialog.setVisible(false); 119 } 120 }); 121 boxMain.add(Box.createHorizontalStrut(40)); 122 boxSaveCancel.add(buttonSave); 123 boxSaveCancel.add(Box.createHorizontalStrut(25)); 124 boxSaveCancel.add(buttonCancel); 125 126 boxMain.add(boxConfigureName); 127 boxMain.add(Box.createVerticalStrut(5)); 128 boxMain.add(boxConfig); 129 boxMain.add(Box.createVerticalStrut(15)); 130 boxMain.add(boxChk); 131 boxMain.add(Box.createVerticalStrut(8)); 132 boxMain.add(boxSaveCancel); 133 134 dialog.getContentPane().add(boxMain); 135 dialog.setResizable(false); 136 dialog.pack(); 137 Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight()); 138 } 139 140 private JFileChooser selectFile() { 141 JFileChooser fc = new JFileChooser(SceneSetting.v().getConfigurationPath()); 142 if (fc.showSaveDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) { 143 return fc; 144 } 145 return null; 146 } 147 148 private void dialogSave() { 149 saveConfiguration(); 150 dialog.setVisible(false); 151 } 152 153 private boolean validate() { 154 if ((textConfigureFile.getText() == null) || (textConfigureName.getText() == null)) { 155 Scene.showErrorMessage("User must name the configuration to be saved and choose a file!"); 156 return false; 157 } 158 159 return true; 160 } 161 162 private void saveConfiguration() { 163 ArrayList vizList = Scene.getVisualizationManager().getVisualizationList(Scene.getDataSourceManager().getCurrentDataSourceId()); 164 if (vizList.size() == 0) { 165 Scene.showErrorMessage("No visualization available for the current data source."); 166 return; 167 } 168 169 XMLWriter writer = new XMLWriter(); 170 writer.initialWriter(textConfigureFile.getText().trim(),false); 171 SerializedData data = new SerializedData(); 172 data.EVolveVersion = Scene.VERSION; 173 data.FileType = "Configuration File"; 174 data.AutoLoadTrace = checkSelection.isSelected() ? "Yes" : "No"; 175 data.ConfigurationName = textConfigureName.getText().trim(); 176 data.DataSourceName = Scene.getDataManager().getDataSource().getClass().getName(); 177 data.NumberOfVisualizations = String.valueOf(vizList.size()); 178 data.NumberOfSelections = String.valueOf(Scene.getFilter().getSelection().length); 179 data.SelectedSelection = String.valueOf(Scene.getFilter().getSelectedIndex()); 180 data.PathForResult = null; 181 data.TraceFileName = Scene.getCurrentDataFilename(); 182 data.SerializedVisualizations = new ArrayList(); 183 data.SerializedSelections = Scene.getFilter().getSelections(); 184 for (int i=0; i<vizList.size(); i++) { 185 Visualization visual = (Visualization)vizList.get(i); 186 data.SerializedVisualizations.add(visual.getCurrentConfiguration()); 187 } 188 XMLWriteOrder orderMap = new XMLWriteOrder(); 189 writer.writeObject(data,orderMap.getOrder(data.getClass().getName())); 190 writer.finalizeWriter(); 191 Scene.getToolsManager().getPredefinedVisualizationRunner().addPredefinedVisualization(textConfigureFile.getText(),data.ConfigurationName); 192 //Scene.getUIManager().refreshMenu(textConfigureFile.getText().trim(),data.ConfigurationName); 193 } 194 195 public void loadConfiguration() { 196 JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastConfigDir()); 197 198 if(fc.showOpenDialog(Scene.getFrame()) != JFileChooser.APPROVE_OPTION) return; 199 200 loadConfiguration(fc.getSelectedFile().getPath()); 201 } 202 203 public void loadConfiguration(String filename) { 204 String configureName = null; 205 206 try { 207 Scene.getUIManager().setLastConfigDir(filename); 208 SerializedData data = getVizInfoFromDisk(filename); 209 configureName = data.ConfigurationName; 210 } catch (Exception e) { 211 Scene.showErrorMessage(e.getMessage()); 212 return; 213 } 214 //Scene.getUIManager().refreshMenu(filename,configureName); 215 Scene.getToolsManager().getPredefinedVisualizationRunner().addPredefinedVisualization(filename,configureName); 216 } 217 218 public SerializedData getVizInfoFromDisk(String filename) throws EVolveException { 219 SerializedData data[] = null; 220 221 try { 222 data = new SerializedData[1]; 223 data[0] = new SerializedData(); 224 XMLLoader reader = new XMLLoader(); 225 reader.initialReader(filename, data[0]); 226 reader.read(data); 227 228 /*if (!data[0].EVolveVersion.equals(Scene.VERSION)) 229 throw (new WrongVersionException(data[0].EVolveVersion));*/ 230 if (data[0].FileType.charAt(0) != 'C') 231 throw new FileTypeMismatchException("Configuration file", data[0].FileType + " file"); 232 233 // read in configurations 234 int vizNumber = Integer.parseInt(data[0].NumberOfVisualizations); 235 SerializedVisualization[] viz = new SerializedVisualization[vizNumber]; 236 for (int i=0; i<vizNumber; i++) 237 viz[i] = new SerializedVisualization(); 238 reader = new XMLLoader(); 239 reader.initialReader(filename, viz[0]); 240 reader.read(viz); 241 data[0].SerializedVisualizations = new ArrayList(); 242 data[0].SerializedSelections = new ArrayList(); 243 for (int i=0; i<viz.length; i++) { 244 data[0].SerializedVisualizations.add(viz[i]); 245 } 246 247 // read in selections 248 int selectionNumber = Integer.parseInt(data[0].NumberOfSelections); 249 if (selectionNumber != 0) { 250 SerializedSelection[] selectionz = new SerializedSelection[selectionNumber]; 251 for (int i=0; i<selectionz.length; i++) 252 selectionz[i] = new SerializedSelection(); 253 reader = new XMLLoader(); 254 reader.initialReader(filename,selectionz[0]); 255 reader.read(selectionz); 256 for (int i=0; i<selectionz.length; i++) { 257 data[0].SerializedSelections.add(selectionz[i]); 258 } 259 } 260 261 /* 262 if (data[0].AutoLoadTrace.equals("Yes")) { 263 if (!Scene.getCurrentDataFilename().equals(data[0].TraceFileName)) { 264 Scene.setDataFilename(data[0].TraceFileName); 265 Scene.autoLoadDataSource(); 266 Scene.setDataFilename(null); 267 } 268 Scene.getFilter().loadSelection(data[0].SerializedSelections); 269 } 270 */ 271 272 } catch (EVolveException e) { 273 String detail = e.getMessage(); 274 throw new ReadConfigureFileException(filename, "N/A" ,"Detailed Information:\n"+detail); 275 } catch (IOException e) { 276 Scene.showErrorMessage(e.getMessage()); 277 } catch (Exception e) { 278 throw new ReadConfigureFileException(filename, "N/A" ,"Detailed Information:N/A"); 279 } 280 281 return data[0]; 282 } 283 }