| 1 package rex.graphics.mdxeditor.mdxbuilder; 2 3 4 import java.util.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 import rex.utils.*; 9 import java.util.Stack ; 10 import rex.utils.S; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.ByteArrayInputStream ; 13 import java.io.FileInputStream ; 14 import java.io.File ; 15 import java.io.FileOutputStream ; 16 import java.io.FileNotFoundException ; 17 18 23 public class MdxBuilderToolbar extends JPanel{ 24 private CanSaveAndResumeState parent; 25 private HandlesMdxEditorSettings settingsHandler; 26 private JLabel saveQuery 27 , loadQuery 28 , undoAction 29 , redoAction 30 , newQuery; 31 private static int MAX_UNDO_OPERATIONS = 20; 32 private Stack undoStack 33 , redoStack; 34 35 37 38 private static ImageIcon undoEnabledIcon; 39 private static ImageIcon undoDisabledIcon; 40 private static ImageIcon redoEnabledIcon; 41 private static ImageIcon redoDisabledIcon; 42 static { 43 44 50 undoEnabledIcon = S.getAppIcon("Undo16.gif"); 51 undoDisabledIcon = S.getAppIcon("Undodim16.gif"); 52 redoEnabledIcon = S.getAppIcon("Redo16.gif"); 53 redoDisabledIcon = S.getAppIcon("Redodim16.gif"); 54 57 } 58 59 63 public MdxBuilderToolbar(CanSaveAndResumeState _parent, HandlesMdxEditorSettings _settingsHandler) { 64 65 66 parent = _parent; 67 settingsHandler = _settingsHandler; 68 69 75 newQuery = new JLabel(S.getAppIcon("New16.gif")); 76 79 newQuery.addMouseListener(new MouseAdapter() { 80 public void mouseClicked(MouseEvent e) { 81 MdxBuilderToolbar.this.newQuery(); 82 } 83 }); 84 newQuery.setToolTipText("New Query"); 85 86 87 93 saveQuery = new JLabel(S.getAppIcon("Save16.gif")); 94 97 saveQuery.addMouseListener(new MouseAdapter() { 98 public void mouseClicked(MouseEvent e) { 99 MdxBuilderToolbar.this.saveQuery(); 100 } 101 }); 102 saveQuery.setToolTipText("Save Query"); 103 104 110 loadQuery = new JLabel(S.getAppIcon("Open16.gif")); 111 114 loadQuery.addMouseListener(new MouseAdapter() { 115 public void mouseClicked(MouseEvent e) { 116 MdxBuilderToolbar.this.loadQuery(); 117 } 118 }); 119 loadQuery.setToolTipText("Load saved query"); 120 loadQuery.setOpaque(false); 121 122 123 undoAction = new JLabel(undoDisabledIcon); 124 undoAction.addMouseListener(new MouseAdapter() { 125 public void mouseClicked(MouseEvent e) { 126 MdxBuilderToolbar.this.undo(); 127 } 128 }); 129 undoAction.setToolTipText("Undo action"); 130 undoAction.setOpaque(false); 131 133 redoAction = new JLabel(redoDisabledIcon); 134 redoAction.addMouseListener(new MouseAdapter() { 135 public void mouseClicked(MouseEvent e) { 136 MdxBuilderToolbar.this.redo(); 137 } 138 }); 139 redoAction.setToolTipText("Redo action"); 140 redoAction.setOpaque(false); 141 143 144 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 146 this.add(Box.createRigidArea(new Dimension(4, 0))); 147 this.add(newQuery); 148 this.add(Box.createRigidArea(new Dimension(4, 0))); 149 this.add(saveQuery); 150 this.add(Box.createRigidArea(new Dimension(4, 0))); 151 this.add(loadQuery); 152 this.add(Box.createRigidArea(new Dimension(4, 0))); 153 this.add(undoAction); 154 this.add(Box.createRigidArea(new Dimension(4, 0))); 155 this.add(redoAction); 156 this.setBorder(AppColors.TOOLBAR_BORDER); 157 this.setOpaque(false); 158 159 160 undoStack = new Stack (); 161 redoStack = new Stack (); 162 163 } 164 168 public void setUndoEnabled(boolean enabled){ 169 undoAction.setEnabled(enabled); 170 if (enabled){ 172 undoAction.setIcon(undoEnabledIcon); 173 }else{ 174 undoAction.setIcon(undoDisabledIcon); 175 } 176 } 177 178 182 public void setRedoEnabled(boolean enabled){ 183 redoAction.setEnabled(enabled); 184 if (enabled){ 185 redoAction.setIcon(redoEnabledIcon); 186 }else{ 187 redoAction.setIcon(redoDisabledIcon); 188 } 189 } 190 191 194 public void refreshUndoRedoState(){ 195 setUndoEnabled(canUndo()); 196 setRedoEnabled(canRedo()); 197 } 198 199 203 public void actionToBePerformed(){ 204 if (undoStack.size() == MAX_UNDO_OPERATIONS) 205 undoStack.removeElementAt(0); 206 ByteArrayOutputStream memStream = new ByteArrayOutputStream (); 207 redoStack.removeAllElements(); 208 parent.saveState(memStream); 209 undoStack.push(memStream); 210 refreshUndoRedoState(); 211 } 212 213 216 public void undo(){ 217 if (canUndo()){ 218 ByteArrayOutputStream currMemStream = new ByteArrayOutputStream (); 220 parent.saveState(currMemStream); 221 redoStack.push(currMemStream); 222 ByteArrayOutputStream memStream = (ByteArrayOutputStream )undoStack.pop(); 224 parent.resumeState(new ByteArrayInputStream (memStream.toByteArray())); 225 refreshUndoRedoState(); 226 }else{ 227 } 229 } 230 231 234 public void redo(){ 235 if (canRedo()){ 236 ByteArrayOutputStream currMemStream = new ByteArrayOutputStream (); 238 parent.saveState(currMemStream); 239 undoStack.push(currMemStream); 240 ByteArrayOutputStream memStream = (ByteArrayOutputStream )redoStack.pop(); 242 parent.resumeState(new ByteArrayInputStream (memStream.toByteArray())); 244 refreshUndoRedoState(); 245 }else{ 246 } 248 } 249 250 254 public boolean canUndo(){ 255 return !undoStack.empty(); 256 } 257 258 262 public boolean canRedo(){ 263 return !redoStack.empty(); 264 } 265 266 269 protected void newQuery(){ 270 parent.clearState(); 271 } 272 273 276 protected void loadQuery(){ 277 JFileChooser chooser = new JFileChooser(); 278 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 279 if (settingsHandler.getDefaultSaveDirectory() != null) 280 chooser.setCurrentDirectory(new File (settingsHandler.getDefaultSaveDirectory())); 281 int returnVal = chooser.showOpenDialog(null); 282 if (returnVal != JFileChooser.APPROVE_OPTION) { 283 return; 284 } 285 FileInputStream in; 286 try{ 287 in = new FileInputStream (chooser.getSelectedFile().getAbsolutePath()); 288 }catch(Exception e){ 289 e.printStackTrace(); 290 JOptionPane.showMessageDialog(null 291 , "Sorry, error while loading a query:" + chooser.getSelectedFile().getAbsolutePath() 292 , "Loading query" 293 , JOptionPane.ERROR_MESSAGE); 294 return; 295 } 296 297 parent.resumeState(in); 298 } 299 300 303 protected void saveQuery(){ 304 JFileChooser chooser = new JFileChooser(); 305 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 306 if (settingsHandler.getDefaultSaveDirectory() != null) 307 chooser.setCurrentDirectory(new File (settingsHandler.getDefaultSaveDirectory())); 308 int returnVal = chooser.showSaveDialog(null); 309 if (returnVal != JFileChooser.APPROVE_OPTION) { 310 return; 311 } 312 FileOutputStream out = null; 313 try { 314 out = new FileOutputStream (chooser.getSelectedFile().getAbsolutePath()); 315 } catch (FileNotFoundException e){ 316 JOptionPane.showMessageDialog(null 317 , "Query is not saved!\nFile not found:" + chooser.getSelectedFile().getAbsolutePath() 318 , "Saving query." 319 , JOptionPane.ERROR_MESSAGE); 320 return; 321 322 } 323 parent.saveState(out); 324 JOptionPane.showMessageDialog(null 325 , "Query saved!" 326 , "Saving query" 327 , JOptionPane.INFORMATION_MESSAGE); 328 settingsHandler.setDefaultSaveDirectory(chooser.getSelectedFile().getParentFile().getAbsolutePath()); 329 330 } 331 332 336 public void paintComponent(Graphics g) { 337 S.paintBackground(g, this); 338 super.paintComponent(g); 339 } 340 341 } 342 | Popular Tags |