4
import java.awt.event.*;
8
import javax.swing.event.ChangeEvent;
9
import javax.swing.event.ChangeListener;
12
* This class builds the Toolbox (GUI component). It includes the ToolPanel as well as the ToolSettingsPanel.
14
public class Toolbox extends JPanel{
16
// These constant will help to call the appropriate Event in the listener method
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;
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;
32
// Toolbar Image handling
33
public boolean aToolSelected;
34
public JLabel lastToolSelected;
35
public String lastImageName;
38
// Directory where the images are located
39
public static final String IMAGE_FOLDER = "images/toolbox/";
42
public Tool curtool() {
44
switch (getCurrentTool()) {
46
// Create Brush Instance
47
selectedTool = new Brush(false);
50
// Create Eraser Instance
51
selectedTool = new Brush(true);
54
selectedTool = new SegsCopyTool();
55
UITop.viewContainer.setCursor(Cursor.getDefaultCursor());
59
selectedTool = new ResizerTool();
60
UITop.viewContainer.setCursor(Cursor.getDefaultCursor());
64
UITop.viewContainer.setCursor(Cursor.getDefaultCursor());
73
if (mySelectedTool == null){
79
// return new Brush(false);
82
public void mouseClicked(Coord c, int button, View view)
86
public void mouseDragged(Coord c, View view)
90
public void mouseReleased(Coord c, int button, View view)
94
public java.awt.Component panel()
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/
109
// Toolbox represents all the left menu
110
toolbox = new JPanel(new BorderLayout());
111
toolbox.setBorder(BorderFactory.createRaisedBevelBorder());
113
JPanel toolboxPanel = new JPanel();
114
toolboxPanel.setLayout(new GridBagLayout());
115
toolboxPanel.setBackground(new Color(242, 242, 242));
116
buildIcons(toolboxPanel);
118
toolbox.add(toolboxPanel, BorderLayout.NORTH);
120
// Settings Panel is the middle of the left menu
121
settingsPanel = new JPanel();
122
settingsPanel.setBackground(new Color(242, 242, 242));
124
settingsPanel.setPreferredSize(new Dimension(180, 200));
126
EventHandler.register(ProjectChange.class,new EventListener<ProjectChange>(){
127
public void handle(ProjectChange ev){
128
if (ev.var.equals("new"))
130
buildSettingsPanel(currentTool);
135
toolbox.add(settingsPanel, BorderLayout.CENTER);
139
* Getter for the Toolbox main Panel. It is used by UITop to get back all the content built in this class
141
public JPanel getToolbox(){
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)
149
public int getCurrentTool(){
150
return this.currentTool;
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)
156
public void setCurrentTool(int newTool){
157
this.currentTool=newTool;
158
// Update the same method that returns a Tool
163
* Getter for the settingsPanel. Used by IconMouseListener and the Tool subclasses
165
public static JPanel getSettingsPanel(){
166
return settingsPanel;
170
* This method reset the settingsPanel (It changes for each tool).
172
public static void emptySettingsPanel(){
173
settingsPanel.removeAll();
174
settingsPanel.setBackground(new Color(242, 242, 242));
175
settingsPanel.revalidate();
176
settingsPanel.repaint();
180
* This method builds the ToolbarIcon. It checks if the image is available so no crash should happen.
182
public JLabel makeToolbarIcon(String imgName, String imgFolder, String toolTipText, String altText, int id) {
183
JLabel myIcon = new JLabel();
184
myIcon.setToolTipText(toolTipText);
186
// Look for the image.
188
URL imageURL=getClass().getResource(imgFolder+imgName+"_default.png");
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");
202
// Add the Mouse Listener
203
myIcon.addMouseListener(new IconMouseListener(myIcon, imgName, imgFolder, id));
208
* This Methods allow to create an ImageIcon for an ImageIcon object. Returns an ImageIcon, or null if the path was invalid.
210
public ImageIcon createImageIcon(String path) {
211
java.net.URL imgURL = getClass().getResource(path);
212
if (imgURL != null) {
213
return new ImageIcon(imgURL);
215
System.err.println("Couldn't find file: " + path);
221
* buildIcons place each JLabel and call makeToolbarIcon for its construction
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);
228
// Create and initialize the icon.
230
JLabel currentIcon = null;
233
currentIcon = makeToolbarIcon("brush", IMAGE_FOLDER, "Paint a segmentation with a brush", "IconBrush", ID_BRUSH);
236
myLeftPanel.add(currentIcon,c);
239
currentIcon = makeToolbarIcon("eraser", IMAGE_FOLDER, "Remove a segmentation with an eraser", "IconEraser", ID_ERASER);
241
myLeftPanel.add(currentIcon,c);
244
currentIcon = makeToolbarIcon("copy", IMAGE_FOLDER, "Copy a segmented slice", "IconCopy", ID_COPY);
245
c.insets = new Insets(10,0,10,10);
247
myLeftPanel.add(currentIcon,c);
251
currentIcon = makeToolbarIcon("mouse", IMAGE_FOLDER, "Select default mouse tool", "IconMouse", ID_MOUSE);
252
c.insets = new Insets(10,0,0,0);
255
myLeftPanel.add(currentIcon,c);
258
currentIcon = makeToolbarIcon("resize", IMAGE_FOLDER, "Resize the image", "IconResize", ID_RESIZE);
260
myLeftPanel.add(currentIcon,c);
263
// Vertical separator
265
c.insets = new Insets(0,0,0,10);
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")));
274
System.err.println("Resource not found: "+ IMAGE_FOLDER+ "vertical_separator_100.png");
276
myLeftPanel.add(verticalSeparator,c);
278
// Horizontal separator
280
c.insets = new Insets(0,0,0,0);
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")));
289
System.err.println("Resource not found: "+ IMAGE_FOLDER+ "horizontal_separator_60.png");
291
myLeftPanel.add(horizontalSeparator,c);
296
* buildJLabelIcon aim to check whether the image exists to create the JLabel. It avoids duplication of this code
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)));
303
myJLabel.setText(AltText);
304
System.err.println("Resource not found: "+ imgFolder+ imgName);
311
* buildSettingsPanel will update the JPanel settingsPanel depending on the selected tool's options.
313
public void buildSettingsPanel(int idTool) {
314
// Start by cleaning what was here before
315
emptySettingsPanel();
317
JPanel myPanel=getSettingsPanel();
318
myPanel.setLayout(new GridBagLayout());
320
GridBagConstraints cc = new GridBagConstraints();
321
//cc.fill = GridBagConstraints.VERTICAL;
322
cc.insets = new Insets(10,0,10,20);
324
// Switch structure to handle the different tools
327
myPanel = (JPanel) selectedTool.panel();
330
myPanel = (JPanel) selectedTool.panel();
333
myPanel = (JPanel) selectedTool.panel();
337
myPanel = (JPanel) selectedTool.panel();
339
myPanel.setBackground(Color.GREEN);
340
JLabel prout = new JLabel("...in the wall");
343
myPanel.add(prout,cc);
356
// this revalidate method is kind of a holy Grail in Swing. Without this call, the JPanel never updates!
357
myPanel.revalidate();
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
369
public class IconMouseListener implements MouseListener{
373
String imageFolder="";
377
* Constructor for the method, assign variables
379
public IconMouseListener(JLabel myIcon, String imgName, String imgFolder, int num) {
380
this.currentIcon=myIcon;
381
this.imageName=imgName;
382
this.imageFolder=imgFolder;
387
* mouseClick event. Calls nothing as it is better to rely on MousePressed Event
389
public void mouseClicked(MouseEvent arg0) {
393
* mouseEntered event. Time to change Icon
395
public void mouseEntered(MouseEvent arg0) {
396
if (lastID == this.id){
399
this.currentIcon = buildJLabelIcon(this.imageName+"_over.png", this.imageFolder, "", this.currentIcon);
405
* mouseExited event. Time to change Icon
407
public void mouseExited(MouseEvent arg0) {
408
if (lastID == this.id){
411
this.currentIcon = buildJLabelIcon(this.imageName+"_default.png", this.imageFolder, "", this.currentIcon);
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.
422
public void mousePressed(MouseEvent arg0) {
424
// TODO Auto-generated method stub
426
lastToolSelected = currentIcon;
428
lastImageName = this.imageName;
429
aToolSelected = true;
432
if (lastID != this.id){
433
lastToolSelected = buildJLabelIcon(lastImageName+"_default.png", this.imageFolder, "", lastToolSelected);
435
lastToolSelected = currentIcon;
437
lastImageName = this.imageName;
440
this.currentIcon = buildJLabelIcon(this.imageName+"_clicked.png", this.imageFolder, "", this.currentIcon);
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);
450
* mouseReleased event. Nothing to do with it so far
452
public void mouseReleased(MouseEvent arg0) {