~fredrik-dolda2000/sigseg/master

« back to all changes in this revision

Viewing changes to src/sigseg/Toolbox.java

  • Committer: Fredrik Tolf
  • Date: 2009-05-21 05:16:29 UTC
  • Revision ID: fredrik@dolda2000.com-20090521051629-kpmi96wk5bib2ezy
Initial commit of existing code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package sigseg;
 
2
 
 
3
import java.awt.*;
 
4
import java.awt.event.*;
 
5
import java.net.URL;
 
6
 
 
7
import javax.swing.*;
 
8
import javax.swing.event.ChangeEvent;
 
9
import javax.swing.event.ChangeListener;
 
10
 
 
11
/**
 
12
 * This class builds the Toolbox (GUI component). It includes the ToolPanel as well as the ToolSettingsPanel.
 
13
 */
 
14
public class Toolbox extends JPanel{
 
15
 
 
16
        // These constant will help to call the appropriate Event in the listener method 
 
17
    
 
18
        public static final int ID_BRUSH=1;
 
19
        public static final int ID_ERASER=2;
 
20
        public static final int ID_COPY=3;
 
21
        public static final int ID_RESIZE=4;
 
22
        public static final int ID_MOUSE=10;
 
23
        
 
24
 
 
25
        
 
26
        public int currentTool=ID_MOUSE; //Mouse is the default tool
 
27
        public JPanel toolbox;
 
28
        private static JPanel settingsPanel;
 
29
    public static Tool selectedTool = null;
 
30
 
 
31
    
 
32
    // Toolbar Image handling
 
33
    public boolean aToolSelected;
 
34
    public JLabel lastToolSelected;
 
35
    public String lastImageName;
 
36
    public int lastID;
 
37
    
 
38
        // Directory where the images are located
 
39
        public static final String IMAGE_FOLDER = "images/toolbox/";
 
40
 
 
41
        
 
42
        public Tool curtool() {
 
43
                selectedTool=null;
 
44
                switch (getCurrentTool()) {
 
45
                case ID_BRUSH:
 
46
                        // Create Brush Instance
 
47
                        selectedTool = new Brush(false);
 
48
                        break;
 
49
                case ID_ERASER:
 
50
                        // Create Eraser Instance
 
51
                        selectedTool = new Brush(true);
 
52
                        break;
 
53
                case ID_COPY:
 
54
                        selectedTool = new SegsCopyTool();
 
55
                UITop.viewContainer.setCursor(Cursor.getDefaultCursor());
 
56
                        break;
 
57
 
 
58
                case ID_RESIZE :
 
59
                        selectedTool = new ResizerTool();
 
60
                        UITop.viewContainer.setCursor(Cursor.getDefaultCursor());
 
61
                        break;
 
62
 
 
63
                case ID_MOUSE :
 
64
                        UITop.viewContainer.setCursor(Cursor.getDefaultCursor());
 
65
                        break;
 
66
 
 
67
                default: break;
 
68
 
 
69
        }
 
70
                
 
71
                return selectedTool;
 
72
                /*
 
73
                if (mySelectedTool == null){
 
74
                        mySelectedTool
 
75
                }
 
76
                
 
77
                
 
78
                
 
79
                // return new Brush(false);
 
80
                /*return new Tool()
 
81
                {
 
82
                        public void mouseClicked(Coord c, int button, View view)
 
83
                        {
 
84
                                return;
 
85
                        }
 
86
                    public void mouseDragged(Coord c, View view)
 
87
                    {
 
88
                        return;
 
89
                    }
 
90
                    public void mouseReleased(Coord c, int button, View view)
 
91
                    {
 
92
                        return;
 
93
                    }
 
94
                    public java.awt.Component panel()
 
95
                    {
 
96
                        return null;
 
97
                    }
 
98
                };*/
 
99
        }
 
100
 
 
101
        /**
 
102
         * Constructor of the class Toolbox. It builds a main JPanel (toolbox) into which are placed two other JPanels:
 
103
         *      .toolboxPanel where the ToolPanel is built
 
104
         *      .settingsPanel where the ToolSettingsPanel is made
 
105
         *  Fortunately the variable names make it easy to understand, right? \o/
 
106
         */
 
107
        public Toolbox(){
 
108
 
 
109
                // Toolbox represents all the left menu
 
110
                toolbox = new JPanel(new BorderLayout());
 
111
                toolbox.setBorder(BorderFactory.createRaisedBevelBorder());
 
112
 
 
113
                JPanel toolboxPanel = new JPanel();
 
114
                toolboxPanel.setLayout(new GridBagLayout());
 
115
                toolboxPanel.setBackground(new Color(242, 242, 242));           
 
116
                buildIcons(toolboxPanel);
 
117
 
 
118
                toolbox.add(toolboxPanel, BorderLayout.NORTH);
 
119
 
 
120
                // Settings Panel is the middle of the left menu
 
121
                settingsPanel = new JPanel();
 
122
                settingsPanel.setBackground(new Color(242, 242, 242));
 
123
                // debug                
 
124
                settingsPanel.setPreferredSize(new Dimension(180, 200));
 
125
                
 
126
                EventHandler.register(ProjectChange.class,new EventListener<ProjectChange>(){   
 
127
                        public void handle(ProjectChange ev){
 
128
                                if (ev.var.equals("new"))
 
129
                        {
 
130
                                        buildSettingsPanel(currentTool);
 
131
                        } 
 
132
                        }
 
133
                });
 
134
 
 
135
                toolbox.add(settingsPanel, BorderLayout.CENTER);
 
136
        }
 
137
 
 
138
        /** 
 
139
         * Getter for the Toolbox main Panel. It is used by UITop to get back all the content built in this class
 
140
         */
 
141
        public JPanel getToolbox(){
 
142
                return toolbox;
 
143
        }
 
144
 
 
145
        /** 
 
146
         * Getter to know which tool is currently selected. It gives back a int that can be compared (with a switch structure) 
 
147
         * to the different constants (ID_BRUSH, ID_CONTRAST... etc)
 
148
         */
 
149
        public int getCurrentTool(){
 
150
                return this.currentTool;
 
151
        }
 
152
 
 
153
        /** 
 
154
         * Setter to save which tool is currently selected. It takes a int as input, that should be a constant (ID_BRUSH, ID_CONTRAST... etc)
 
155
         */
 
156
        public void setCurrentTool(int newTool){
 
157
                this.currentTool=newTool;
 
158
                // Update the same method that returns a Tool
 
159
                curtool();
 
160
        }
 
161
 
 
162
        /** 
 
163
         * Getter for the settingsPanel. Used by IconMouseListener and the Tool subclasses
 
164
         */
 
165
        public static JPanel getSettingsPanel(){
 
166
                return settingsPanel;
 
167
        }
 
168
        
 
169
        /** 
 
170
         * This method reset the settingsPanel (It changes for each tool).
 
171
         */
 
172
        public static void emptySettingsPanel(){
 
173
                settingsPanel.removeAll();
 
174
                settingsPanel.setBackground(new Color(242, 242, 242));
 
175
                settingsPanel.revalidate();
 
176
                settingsPanel.repaint();
 
177
        }
 
178
 
 
179
        /** 
 
180
         * This method builds the ToolbarIcon. It checks if the image is available so no crash should happen.
 
181
         */
 
182
        public JLabel makeToolbarIcon(String imgName, String imgFolder, String toolTipText, String altText, int id) {
 
183
                JLabel  myIcon = new JLabel();
 
184
                myIcon.setToolTipText(toolTipText);
 
185
 
 
186
                //      Look for the image.
 
187
 
 
188
                URL imageURL=getClass().getResource(imgFolder+imgName+"_default.png");
 
189
 
 
190
                if (imageURL != null) {   //image found
 
191
                        ImageIcon myImageIcon = new ImageIcon(imageURL);
 
192
                        myIcon.setIcon(myImageIcon);
 
193
                } else if (getClass().getResource(imgFolder+"imageMissing.png") != null){  // ImageIcon not found but errorImage found
 
194
                        ImageIcon myImageIcon = new ImageIcon(getClass().getResource(imgFolder+"imageMissing.png"));
 
195
                        myIcon.setIcon(myImageIcon);
 
196
                        System.err.println("Resource not found: "+ imgFolder+"imageMissing.png");
 
197
                } else { //no image found: show text!
 
198
                        myIcon.setText(altText);
 
199
                        System.err.println("Resource not found: "+ imgFolder+imgName+"_default.png");
 
200
                }
 
201
 
 
202
                // Add the Mouse Listener
 
203
                myIcon.addMouseListener(new IconMouseListener(myIcon, imgName, imgFolder, id));
 
204
                return myIcon;
 
205
        }
 
206
 
 
207
        /** 
 
208
         * This Methods allow to create an ImageIcon for an ImageIcon object. Returns an ImageIcon, or null if the path was invalid. 
 
209
         * */
 
210
        public ImageIcon createImageIcon(String path) {
 
211
                java.net.URL imgURL = getClass().getResource(path);
 
212
                if (imgURL != null) {
 
213
                        return new ImageIcon(imgURL);
 
214
                } else {
 
215
                        System.err.println("Couldn't find file: " + path);
 
216
                        return null;
 
217
                }
 
218
        }
 
219
 
 
220
        /**
 
221
         * buildIcons place each JLabel and call makeToolbarIcon for its construction
 
222
         * */
 
223
        public void buildIcons(JPanel myLeftPanel) {
 
224
                GridBagConstraints c = new GridBagConstraints();
 
225
                //c.fill = GridBagConstraints.VERTICAL;
 
226
                c.insets = new Insets(10,0,0,10);
 
227
 
 
228
                //      Create and initialize the icon.
 
229
 
 
230
                JLabel currentIcon = null;
 
231
 
 
232
                //brush icon
 
233
                currentIcon = makeToolbarIcon("brush", IMAGE_FOLDER, "Paint a segmentation with a brush", "IconBrush", ID_BRUSH);
 
234
                c.gridx = 0;
 
235
                c.gridy = 0; 
 
236
                myLeftPanel.add(currentIcon,c);
 
237
                
 
238
                //eraser icon
 
239
                currentIcon = makeToolbarIcon("eraser", IMAGE_FOLDER, "Remove a segmentation with an eraser", "IconEraser", ID_ERASER);
 
240
                c.gridy = 1; 
 
241
                myLeftPanel.add(currentIcon,c);
 
242
 
 
243
                //copy icon
 
244
                currentIcon = makeToolbarIcon("copy", IMAGE_FOLDER, "Copy a segmented slice", "IconCopy", ID_COPY);
 
245
                c.insets = new Insets(10,0,10,10);
 
246
                c.gridy = 2;
 
247
                myLeftPanel.add(currentIcon,c);
 
248
 
 
249
 
 
250
                //mouse icon
 
251
                currentIcon = makeToolbarIcon("mouse", IMAGE_FOLDER, "Select default mouse tool", "IconMouse", ID_MOUSE);
 
252
                c.insets = new Insets(10,0,0,0);
 
253
                c.gridx = 2;
 
254
                c.gridy = 0; 
 
255
                myLeftPanel.add(currentIcon,c);
 
256
                
 
257
                //resize icon
 
258
                currentIcon = makeToolbarIcon("resize", IMAGE_FOLDER, "Resize the image", "IconResize", ID_RESIZE);
 
259
                c.gridy = 1;
 
260
                myLeftPanel.add(currentIcon,c);
 
261
 
 
262
 
 
263
                //      Vertical separator
 
264
 
 
265
                c.insets = new Insets(0,0,0,10);
 
266
                c.gridx = 1;
 
267
                c.gridy = 0; 
 
268
                c.gridheight = 4;
 
269
                JLabel verticalSeparator = new JLabel();
 
270
                if (getClass().getResource(IMAGE_FOLDER+ "vertical_separator_100.png") != null){ 
 
271
                        verticalSeparator.setIcon(new ImageIcon(getClass().getResource(IMAGE_FOLDER+ "vertical_separator_100.png")));
 
272
                }
 
273
                else {
 
274
                        System.err.println("Resource not found: "+ IMAGE_FOLDER+ "vertical_separator_100.png");
 
275
                }
 
276
                myLeftPanel.add(verticalSeparator,c);
 
277
 
 
278
                // Horizontal separator
 
279
 
 
280
                c.insets = new Insets(0,0,0,0);
 
281
                c.gridx = 0;
 
282
                c.gridy = 3; 
 
283
                c.gridwidth = 3;
 
284
                JLabel horizontalSeparator = new JLabel();
 
285
                if (getClass().getResource(IMAGE_FOLDER+ "horizontal_separator_60.png") != null){ 
 
286
                        horizontalSeparator.setIcon(new ImageIcon(getClass().getResource(IMAGE_FOLDER+ "horizontal_separator_60.png")));
 
287
                }
 
288
                else {
 
289
                        System.err.println("Resource not found: "+ IMAGE_FOLDER+ "horizontal_separator_60.png");
 
290
                }
 
291
                myLeftPanel.add(horizontalSeparator,c);
 
292
 
 
293
        }
 
294
 
 
295
        /**
 
296
         * buildJLabelIcon aim to check whether the image exists to create the JLabel. It avoids duplication of this code
 
297
         * */
 
298
        public JLabel buildJLabelIcon(String imgName, String imgFolder, String AltText, JLabel myJLabel){
 
299
                if (getClass().getResource(imgFolder+ imgName) != null){ 
 
300
                        myJLabel.setIcon(new ImageIcon(getClass().getResource(imgFolder+ imgName)));
 
301
                }
 
302
                else {
 
303
                        myJLabel.setText(AltText);
 
304
                        System.err.println("Resource not found: "+ imgFolder+ imgName);
 
305
                }
 
306
                return myJLabel;
 
307
        }
 
308
 
 
309
        
 
310
        /**
 
311
         * buildSettingsPanel will update the JPanel settingsPanel depending on the selected tool's options.
 
312
         * */
 
313
        public void buildSettingsPanel(int idTool) {
 
314
                // Start by cleaning what was here before
 
315
                emptySettingsPanel();
 
316
                // get settingsPanel
 
317
                JPanel myPanel=getSettingsPanel();      
 
318
                myPanel.setLayout(new GridBagLayout());
 
319
 
 
320
                GridBagConstraints cc = new GridBagConstraints();
 
321
                //cc.fill = GridBagConstraints.VERTICAL;
 
322
                cc.insets = new Insets(10,0,10,20);
 
323
 
 
324
                // Switch structure to handle the different tools
 
325
        switch (idTool) {
 
326
            case ID_BRUSH:
 
327
                myPanel = (JPanel) selectedTool.panel();
 
328
                break;
 
329
            case ID_ERASER:
 
330
                myPanel = (JPanel) selectedTool.panel();
 
331
                break;
 
332
            case ID_COPY:
 
333
                myPanel = (JPanel) selectedTool.panel();
 
334
                break;
 
335
 
 
336
                case ID_RESIZE :
 
337
                        myPanel = (JPanel) selectedTool.panel();
 
338
                        /*
 
339
                        myPanel.setBackground(Color.GREEN);     
 
340
                        JLabel prout = new JLabel("...in the wall");
 
341
                        cc.gridx = 0;
 
342
                        cc.gridy = 0; 
 
343
                        myPanel.add(prout,cc);
 
344
                        */
 
345
                        break;
 
346
 
 
347
                case ID_MOUSE :
 
348
 
 
349
                        break;
 
350
 
 
351
                default: break;
 
352
 
 
353
                }
 
354
                // Update the JPanel
 
355
                myPanel.updateUI();
 
356
                // this revalidate method is kind of a holy Grail in Swing. Without this call, the JPanel never updates! 
 
357
                myPanel.revalidate();
 
358
                myPanel.repaint();
 
359
        }
 
360
        
 
361
 
 
362
        /** 
 
363
         * This MouseListener is shared by all the toolbarIcons. It does the image swapping (mouse over, click etc) and calls:
 
364
         *              .buildSettingsPanel(idTool) to build the settings Panel corresponding to that tool
 
365
         *              .setCurrentTool(idTool) to update the class variable
 
366
         * Other specific tool events should be handled by a method like buildSettingsPanel(idTool) that has not yet been defined.
 
367
         * It is a inner class
 
368
         * */
 
369
        public class IconMouseListener implements MouseListener{
 
370
 
 
371
                JLabel currentIcon;
 
372
                String imageName="";
 
373
                String imageFolder="";
 
374
                int id=0;
 
375
 
 
376
                /** 
 
377
                 * Constructor for the method, assign variables
 
378
                 * */
 
379
                public IconMouseListener(JLabel myIcon, String imgName, String imgFolder, int num) {
 
380
                        this.currentIcon=myIcon;
 
381
                        this.imageName=imgName;
 
382
                        this.imageFolder=imgFolder;
 
383
                        this.id=num;
 
384
                }
 
385
 
 
386
                /** 
 
387
                 * mouseClick event. Calls nothing as it is better to rely on MousePressed Event
 
388
                 * */
 
389
                public void mouseClicked(MouseEvent arg0) {                                             
 
390
                }
 
391
                
 
392
                /** 
 
393
                 * mouseEntered event. Time to change Icon
 
394
                 * */
 
395
                public void mouseEntered(MouseEvent arg0) {
 
396
                        if (lastID == this.id){
 
397
                        }
 
398
                        else{
 
399
                                this.currentIcon = buildJLabelIcon(this.imageName+"_over.png", this.imageFolder, "", this.currentIcon);
 
400
                        }       
 
401
 
 
402
                }
 
403
 
 
404
                /** 
 
405
                 * mouseExited event. Time to change Icon
 
406
                 * */
 
407
                public void mouseExited(MouseEvent arg0) {
 
408
                        if (lastID == this.id){
 
409
                        }
 
410
                        else{
 
411
                                this.currentIcon = buildJLabelIcon(this.imageName+"_default.png", this.imageFolder, "", this.currentIcon);
 
412
                        }       
 
413
                                
 
414
                        
 
415
                        
 
416
                }
 
417
 
 
418
                /** 
 
419
                 * mousePressed event. It calls the settingsPanel and currentTool updates. 
 
420
                 * We have to do so because the user do not really make a difference between the two events as long as he is aiming for the tool icon.
 
421
                 * */
 
422
        public void mousePressed(MouseEvent arg0) {
 
423
                
 
424
                // TODO Auto-generated method stub
 
425
                        if (!aToolSelected){                    
 
426
                                lastToolSelected = currentIcon;
 
427
                                lastID = this.id;
 
428
                                lastImageName = this.imageName;
 
429
                                aToolSelected = true;
 
430
                        }
 
431
                        else {
 
432
                                if (lastID != this.id){
 
433
                                        lastToolSelected = buildJLabelIcon(lastImageName+"_default.png", this.imageFolder, "", lastToolSelected);
 
434
                                }
 
435
                                lastToolSelected = currentIcon;
 
436
                                lastID = this.id;
 
437
                                lastImageName = this.imageName;
 
438
 
 
439
                        }
 
440
                        this.currentIcon = buildJLabelIcon(this.imageName+"_clicked.png", this.imageFolder, "", this.currentIcon);
 
441
                        
 
442
                    
 
443
                        // Notify that the current tool was changed
 
444
                        setCurrentTool(this.id);
 
445
                        // Call the appropriate listener depending on the Icon used
 
446
                        buildSettingsPanel(this.id);
 
447
        }
 
448
 
 
449
                /** 
 
450
                 * mouseReleased event. Nothing to do with it so far
 
451
                 * */
 
452
                public void mouseReleased(MouseEvent arg0) {            
 
453
                }
 
454
 
 
455
                
 
456
        }       
 
457
 
 
458
 
 
459
}