KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rex > graphics > mdxeditor > mdxbuilder > MdxBuilderToolbar


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 JavaDoc;
10 import rex.utils.S;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17
18 /**
19  * MdxEditor's toolbar component.
20  * @author Igor Mekterovic
21  * @version 0.3
22  */

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 JavaDoc undoStack
33                , redoStack;
34
35 // private UndoableRedoable parent;
36

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     /**
45      * Copyright (C) 2006 CINCOM SYSTEMS, INC.
46      * All Rights Reserved
47      * Copyright (C) 2006 Igor Mekterovic
48      * All Rights Reserved
49      */

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       /*
55        * End of Modification
56        */

57    }
58
59    /**
60     * Creates all the buttons
61     * @param _parent MdxEditor
62     */

63    public MdxBuilderToolbar(CanSaveAndResumeState _parent, HandlesMdxEditorSettings _settingsHandler) {
64
65
66       parent = _parent;
67       settingsHandler = _settingsHandler;
68
69       /**
70        * Copyright (C) 2006 CINCOM SYSTEMS, INC.
71        * All Rights Reserved
72        * Copyright (C) 2006 Igor Mekterovic
73        * All Rights Reserved
74        */

75       newQuery = new JLabel(S.getAppIcon("New16.gif"));
76       /*
77        * End of Modification
78        */

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       /**
88        * Copyright (C) 2006 CINCOM SYSTEMS, INC.
89        * All Rights Reserved
90        * Copyright (C) 2006 Igor Mekterovic
91        * All Rights Reserved
92        */

93       saveQuery = new JLabel(S.getAppIcon("Save16.gif"));
94       /*
95        * End of Modification
96        */

97       saveQuery.addMouseListener(new MouseAdapter() {
98          public void mouseClicked(MouseEvent e) {
99             MdxBuilderToolbar.this.saveQuery();
100          }
101       });
102       saveQuery.setToolTipText("Save Query");
103
104       /**
105        * Copyright (C) 2006 CINCOM SYSTEMS, INC.
106        * All Rights Reserved
107        * Copyright (C) 2006 Igor Mekterovic
108        * All Rights Reserved
109        */

110       loadQuery = new JLabel(S.getAppIcon("Open16.gif"));
111       /*
112        * End of Modification
113        */

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 // undoAction.setEnabled(false);
132

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 // redoAction.setEnabled(false);
142

143
144 // Line 'em up boys...
145
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 JavaDoc();
161       redoStack = new Stack JavaDoc();
162
163    }
164    /**
165     * Sets the undo button to specified state
166     * @param enabled boolean
167     */

168    public void setUndoEnabled(boolean enabled){
169       undoAction.setEnabled(enabled);
170 // S.out("setting undo = " + enabled);
171
if (enabled){
172          undoAction.setIcon(undoEnabledIcon);
173       }else{
174          undoAction.setIcon(undoDisabledIcon);
175       }
176    }
177
178    /**
179     * Sets the redo button to specified state
180     * @param enabled boolean
181     */

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    /**
192     * Refreshes the display(state) of undo and redo buttons.
193     */

194    public void refreshUndoRedoState(){
195       setUndoEnabled(canUndo());
196       setRedoEnabled(canRedo());
197    }
198
199    /**
200     * This method is called by the object that uses this toolbar before it performs any operation.
201     * <br>This method then saves the state of that object and pushes it onto the stack.
202     */

203    public void actionToBePerformed(){
204       if (undoStack.size() == MAX_UNDO_OPERATIONS)
205          undoStack.removeElementAt(0);
206       ByteArrayOutputStream JavaDoc memStream = new ByteArrayOutputStream JavaDoc();
207       redoStack.removeAllElements();
208       parent.saveState(memStream);
209       undoStack.push(memStream);
210       refreshUndoRedoState();
211    }
212
213    /**
214     * Performs undo operation.
215     */

216    public void undo(){
217       if (canUndo()){
218 // First, save current state (in case of redo) and push it on the redo stack
219
ByteArrayOutputStream JavaDoc currMemStream = new ByteArrayOutputStream JavaDoc();
220          parent.saveState(currMemStream);
221          redoStack.push(currMemStream);
222 // Then, retrieve previous state from the undo stack and resume it
223
ByteArrayOutputStream JavaDoc memStream = (ByteArrayOutputStream JavaDoc)undoStack.pop();
224          parent.resumeState(new ByteArrayInputStream JavaDoc(memStream.toByteArray()));
225          refreshUndoRedoState();
226       }else{
227 // S.out("alert: undo called when there is nothing to undo!");
228
}
229    }
230
231    /**
232     * Performs redo operation.
233     */

234    public void redo(){
235       if (canRedo()){
236 // First, save current state (in case of undo) and push it on the UNDO stack
237
ByteArrayOutputStream JavaDoc currMemStream = new ByteArrayOutputStream JavaDoc();
238          parent.saveState(currMemStream);
239          undoStack.push(currMemStream);
240 // Then, retrieve previous state from the REDO stack and resume it
241
ByteArrayOutputStream JavaDoc memStream = (ByteArrayOutputStream JavaDoc)redoStack.pop();
242 // undoStack.push(memStream);
243
parent.resumeState(new ByteArrayInputStream JavaDoc(memStream.toByteArray()));
244          refreshUndoRedoState();
245       }else{
246 // S.out("alert: redo called when there is nothing to redo!");
247
}
248    }
249
250    /**
251     * Returns true if undo operation can be performed, false otherwise.
252     * @return boolean
253     */

254    public boolean canUndo(){
255       return !undoStack.empty();
256    }
257
258    /**
259     * Returns true if redo operation can be performed, false otherwise.
260     * @return boolean
261     */

262    public boolean canRedo(){
263       return !redoStack.empty();
264    }
265
266    /**
267     * Clears a state of the parent object - for a new query to be created.
268     */

269    protected void newQuery(){
270       parent.clearState();
271    }
272
273    /**
274     * Loads a query from the file. (dialog box for choosing a file is presented to user)
275     */

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 JavaDoc(settingsHandler.getDefaultSaveDirectory()));
281       int returnVal = chooser.showOpenDialog(null);
282       if (returnVal != JFileChooser.APPROVE_OPTION) {
283          return;
284       }
285       FileInputStream JavaDoc in;
286       try{
287          in = new FileInputStream JavaDoc(chooser.getSelectedFile().getAbsolutePath());
288       }catch(Exception JavaDoc 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    /**
301     * Saves a query to the file. (dialog box for choosing a file is presented to user)
302     */

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 JavaDoc(settingsHandler.getDefaultSaveDirectory()));
308       int returnVal = chooser.showSaveDialog(null);
309       if (returnVal != JFileChooser.APPROVE_OPTION) {
310          return;
311       }
312       FileOutputStream JavaDoc out = null;
313       try {
314          out = new FileOutputStream JavaDoc(chooser.getSelectedFile().getAbsolutePath());
315       } catch (FileNotFoundException JavaDoc 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    /**
333     * Overrides the <code>paintComponent(Graphics g)<code> to paint gradient blue background.
334     * @param g Graphics
335     */

336    public void paintComponent(Graphics g) {
337       S.paintBackground(g, this);
338       super.paintComponent(g);
339    }
340
341 }
342
Popular Tags