~ubuntu-branches/ubuntu/trusty/weka/trusty-proposed

« back to all changes in this revision

Viewing changes to weka/gui/beans/BeanVisual.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 *    BeanVisual.java
 
19
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.gui.beans;
 
24
 
 
25
import java.awt.BorderLayout;
 
26
import java.awt.Color;
 
27
import java.awt.Dimension;
 
28
import java.awt.Graphics;
 
29
import java.awt.Image;
 
30
import java.awt.Point;
 
31
import java.awt.Toolkit;
 
32
import java.beans.PropertyChangeListener;
 
33
import java.beans.PropertyChangeSupport;
 
34
import java.io.IOException;
 
35
import java.io.ObjectInputStream;
 
36
import java.io.Serializable;
 
37
 
 
38
import javax.swing.ImageIcon;
 
39
import javax.swing.JLabel;
 
40
import javax.swing.JPanel;
 
41
 
 
42
/**
 
43
 * BeanVisual encapsulates icons and label for a given bean. Has methods
 
44
 * to load icons, set label text and toggle between static and animated
 
45
 * versions of a bean's icon.
 
46
 *
 
47
 * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
 
48
 * @version $Revision: 1.9 $
 
49
 * @since 1.0
 
50
 * @see JPanel
 
51
 * @see Serializable
 
52
 */
 
53
public class BeanVisual
 
54
  extends JPanel {
 
55
 
 
56
  /** for serialization */
 
57
  private static final long serialVersionUID = -6677473561687129614L;
 
58
 
 
59
  public static final String ICON_PATH="weka/gui/beans/icons/";
 
60
 
 
61
  public static final int NORTH_CONNECTOR = 0;
 
62
  public static final int SOUTH_CONNECTOR = 1;
 
63
  public static final int EAST_CONNECTOR = 2;
 
64
  public static final int WEST_CONNECTOR = 3;
 
65
 
 
66
  /**
 
67
   * Holds name (including path) of the static icon
 
68
   */
 
69
  protected String m_iconPath;
 
70
 
 
71
  /**
 
72
   * Holds name (including path) of the animated icon
 
73
   */
 
74
  protected String m_animatedIconPath;
 
75
 
 
76
  /**
 
77
   * ImageIcons for the icons. Is transient because for some reason
 
78
   * animated gifs cease to be animated after restoring from serialization.
 
79
   * Icons are re-loaded from source after deserialization
 
80
   */
 
81
  protected transient ImageIcon m_icon;
 
82
  protected transient ImageIcon m_animatedIcon;
 
83
 
 
84
  /**
 
85
   * Name for the bean
 
86
   */
 
87
  protected String m_visualName;
 
88
 
 
89
  protected JLabel m_visualLabel;
 
90
  
 
91
  /**
 
92
   * Container for the icon
 
93
   */
 
94
  //  protected IconHolder m_visualHolder;
 
95
 
 
96
  //  protected JLabel m_textLabel;
 
97
  private boolean m_stationary = true;
 
98
 
 
99
  private PropertyChangeSupport m_pcs = new PropertyChangeSupport(this);
 
100
  
 
101
  private boolean m_displayConnectors = false;
 
102
  private Color m_connectorColor = Color.blue;
 
103
 
 
104
  /**
 
105
   * Constructor
 
106
   *
 
107
   * @param visualName name for the bean
 
108
   * @param iconPath path to the icon file
 
109
   * @param animatedIconPath path to the animated icon file
 
110
   */
 
111
  public BeanVisual(String visualName, String iconPath, 
 
112
                    String animatedIconPath) {
 
113
 
 
114
    loadIcons(iconPath, animatedIconPath);
 
115
    m_visualName = visualName;
 
116
    //    m_textLabel = new JLabel(m_visualName, JLabel.CENTER);
 
117
    m_visualLabel = new JLabel(m_icon);
 
118
 
 
119
    setLayout(new BorderLayout());
 
120
   
 
121
    //    m_visualHolder = new IconHolder(m_visualLabel);
 
122
    
 
123
    add(m_visualLabel, BorderLayout.CENTER);
 
124
    Dimension d = m_visualLabel.getPreferredSize();
 
125
    //      this.setSize((int)d.getWidth()+50, (int)d.getHeight()+50);
 
126
    Dimension d2 = new Dimension((int)d.getWidth() + 10, 
 
127
                                 (int)d.getHeight() + 10);
 
128
    setMinimumSize(d2);
 
129
    setPreferredSize(d2);
 
130
    setMaximumSize(d2);
 
131
  }
 
132
 
 
133
  /**
 
134
   * Reduce this BeanVisual's icon size by the given factor
 
135
   *
 
136
   * @param factor the factor by which to reduce the icon size by
 
137
   */
 
138
  public void scale(int factor) {
 
139
    if (m_icon != null) {
 
140
      removeAll();
 
141
      Image pic = m_icon.getImage();
 
142
      int width = m_icon.getIconWidth();
 
143
      int height = m_icon.getIconHeight();
 
144
      int reduction = width / factor;
 
145
      width -= reduction;
 
146
      height -= reduction;
 
147
      pic = pic.getScaledInstance(width, height, Image.SCALE_SMOOTH);
 
148
      m_icon = new ImageIcon(pic);
 
149
      m_visualLabel = new JLabel(m_icon);
 
150
      add(m_visualLabel, BorderLayout.CENTER);
 
151
      Dimension d = m_visualLabel.getPreferredSize();
 
152
      //      this.setSize((int)d.getWidth()+50, (int)d.getHeight()+50);
 
153
      Dimension d2 = new Dimension((int)d.getWidth() + 10, 
 
154
                                   (int)d.getHeight() + 10);
 
155
      setMinimumSize(d2);
 
156
      setPreferredSize(d2);
 
157
      setMaximumSize(d2);   
 
158
    }
 
159
  }
 
160
 
 
161
  /**
 
162
   * Loads static and animated versions of a beans icons. These are
 
163
   * assumed to be defined in the system resource location (i.e. in the
 
164
   * CLASSPATH). If the named icons do not exist, no changes to the
 
165
   * visual appearance is made. Since default icons for generic
 
166
   * types of beans (eg. DataSource, Classifier etc)
 
167
   * are assumed to exist, it allows developers to add custom icons for
 
168
   * for specific instantiations of these beans 
 
169
   * (eg. J48, DiscretizeFilter etc) at their leisure.
 
170
   *
 
171
   * @param iconPath path to
 
172
   * @param animatedIconPath a <code>String</code> value
 
173
   */
 
174
  public boolean loadIcons(String iconPath, String animatedIconPath) {
 
175
    boolean success = true;
 
176
    java.net.URL imageURL = ClassLoader.getSystemResource(iconPath);
 
177
    if (imageURL == null) {
 
178
      //      System.err.println("Warning: unable to load "+iconPath);
 
179
    } else {
 
180
      Image pic = Toolkit.getDefaultToolkit().
 
181
        getImage(imageURL);
 
182
 
 
183
      m_icon = new ImageIcon(pic);
 
184
      if (m_visualLabel != null) {
 
185
        m_visualLabel.setIcon(m_icon);
 
186
      }
 
187
    }
 
188
    
 
189
    imageURL = ClassLoader.getSystemResource(animatedIconPath);
 
190
    if (imageURL == null) {
 
191
      //      System.err.println("Warning: unable to load "+animatedIconPath);
 
192
      success = false;
 
193
    } else {
 
194
      Image pic2 = Toolkit.getDefaultToolkit().
 
195
        getImage(imageURL);
 
196
      m_animatedIcon = new ImageIcon(pic2);
 
197
    }
 
198
    m_iconPath = iconPath;
 
199
    m_animatedIconPath = animatedIconPath;
 
200
    return success;
 
201
  }
 
202
 
 
203
  /**
 
204
   * Set the label for the visual. Informs any property change listeners
 
205
   *
 
206
   * @param text the label
 
207
   */
 
208
  public void setText(String text) {
 
209
    m_visualName = text;
 
210
    //    m_textLabel.setText(m_visualName);
 
211
    m_pcs.firePropertyChange("label",null,null);
 
212
  }
 
213
 
 
214
  /**
 
215
   * Get the visual's label
 
216
   *
 
217
   * @return a <code>String</code> value
 
218
   */
 
219
  public String getText() {
 
220
    return m_visualName;
 
221
  }
 
222
 
 
223
  /**
 
224
   * Set the static version of the icon
 
225
   *
 
226
   */
 
227
  public void setStatic() {
 
228
    m_visualLabel.setIcon(m_icon);
 
229
  }
 
230
 
 
231
  /**
 
232
   * Set the animated version of the icon
 
233
   *
 
234
   */
 
235
  public void setAnimated() {
 
236
    m_visualLabel.setIcon(m_animatedIcon);
 
237
  }
 
238
 
 
239
  /**
 
240
   * Returns the coordinates of the closest "connector" point to the
 
241
   * supplied point. Coordinates are in the parent containers coordinate
 
242
   * space.
 
243
   *
 
244
   * @param pt the reference point
 
245
   * @return the closest connector point
 
246
   */
 
247
  public Point getClosestConnectorPoint(Point pt) {
 
248
    int sourceX = getParent().getX();
 
249
    int sourceY = getParent().getY();
 
250
    int sourceWidth = getWidth();
 
251
    int sourceHeight = getHeight();
 
252
    int sourceMidX = sourceX + (sourceWidth / 2);
 
253
    int sourceMidY = sourceY + (sourceHeight / 2);
 
254
    int x = (int)pt.getX();
 
255
    int y = (int)pt.getY();
 
256
    
 
257
    Point closest = new Point();
 
258
    int cx = (Math.abs(x - sourceMidX) < Math.abs(y - sourceMidY)) ? 
 
259
      sourceMidX :
 
260
      ((x < sourceMidX) ? sourceX : sourceX + sourceWidth);
 
261
    int cy = (Math.abs(y - sourceMidY) < Math.abs(x - sourceMidX)) ? 
 
262
      sourceMidY :
 
263
      ((y < sourceMidY) ? sourceY : sourceY + sourceHeight) ;
 
264
    closest.setLocation(cx, cy);
 
265
    return closest;
 
266
  }
 
267
 
 
268
  /**
 
269
   * Returns the coordinates of the connector point given a compass point
 
270
   *
 
271
   * @param compassPoint a compass point
 
272
   * @return a <code>Point</code> value
 
273
   */
 
274
  public Point getConnectorPoint(int compassPoint) {
 
275
    int sourceX = getParent().getX();
 
276
    int sourceY = getParent().getY();
 
277
    int sourceWidth = getWidth();
 
278
    int sourceHeight = getHeight();
 
279
    int sourceMidX = sourceX + (sourceWidth / 2);
 
280
    int sourceMidY = sourceY + (sourceHeight / 2);
 
281
 
 
282
    switch (compassPoint) {
 
283
    case NORTH_CONNECTOR : return new Point(sourceMidX, sourceY);
 
284
    case SOUTH_CONNECTOR : return new Point(sourceMidX, sourceY+sourceHeight);
 
285
    case WEST_CONNECTOR :  return new Point(sourceX, sourceMidY);
 
286
    case EAST_CONNECTOR :  return new Point(sourceX+sourceWidth, sourceMidY);
 
287
    default : System.err.println("Unrecognised connectorPoint (BeanVisual)");
 
288
    }
 
289
    return new Point(sourceX, sourceY);
 
290
  }
 
291
 
 
292
  /**
 
293
   * Returns the static icon
 
294
   *
 
295
   * @return an <code>ImageIcon</code> value
 
296
   */
 
297
  public ImageIcon getStaticIcon() {
 
298
    return m_icon;
 
299
  }
 
300
 
 
301
  /**
 
302
   * Returns the animated icon
 
303
   *
 
304
   * @return an <code>ImageIcon</code> value
 
305
   */
 
306
  public ImageIcon getAnimatedIcon() {
 
307
    return m_animatedIcon;
 
308
  }
 
309
 
 
310
  /**
 
311
   * returns the path for the icon
 
312
   * 
 
313
   * @return the path for the icon
 
314
   */
 
315
  public String getIconPath() {
 
316
    return m_iconPath;
 
317
  }
 
318
 
 
319
  /**
 
320
   * returns the path for the animated icon
 
321
   * 
 
322
   * @return the path for the animated icon
 
323
   */
 
324
  public String getAnimatedIconPath() {
 
325
    return m_animatedIconPath;
 
326
  }
 
327
 
 
328
  /**
 
329
   * Turn on/off the connector points
 
330
   *
 
331
   * @param dc a <code>boolean</code> value
 
332
   */
 
333
  public void setDisplayConnectors(boolean dc) {
 
334
    //    m_visualHolder.setDisplayConnectors(dc);
 
335
    m_displayConnectors = dc;
 
336
    m_connectorColor = Color.blue;
 
337
    repaint();
 
338
  }
 
339
 
 
340
  /**
 
341
   * Turn on/off the connector points
 
342
   *
 
343
   * @param dc a <code>boolean</code> value
 
344
   * @param c the Color to use
 
345
   */
 
346
  public void setDisplayConnectors(boolean dc,
 
347
                                   Color c) {
 
348
    setDisplayConnectors(dc);
 
349
    m_connectorColor = c;
 
350
  }
 
351
 
 
352
  /**
 
353
   * Add a listener for property change events
 
354
   *
 
355
   * @param pcl a <code>PropertyChangeListener</code> value
 
356
   */
 
357
  public void addPropertyChangeListener(PropertyChangeListener pcl) {
 
358
    m_pcs.addPropertyChangeListener(pcl);
 
359
  }
 
360
 
 
361
  /**
 
362
   * Remove a property change listener
 
363
   *
 
364
   * @param pcl a <code>PropertyChangeListener</code> value
 
365
   */
 
366
  public void removePropertyChangeListener(PropertyChangeListener pcl) {
 
367
    m_pcs.removePropertyChangeListener(pcl);
 
368
  }
 
369
 
 
370
  public void paintComponent(Graphics gx) {
 
371
    super.paintComponent(gx);
 
372
    if (m_displayConnectors) {
 
373
      gx.setColor(m_connectorColor);
 
374
      
 
375
      int midx = (int)(this.getWidth() / 2.0);
 
376
      int midy = (int)(this.getHeight() / 2.0);
 
377
      gx.fillOval(midx-2, 0, 5, 5);
 
378
      gx.fillOval(midx-2, this.getHeight()-5, 5, 5);
 
379
      gx.fillOval(0, midy-2, 5, 5);
 
380
      gx.fillOval(this.getWidth()-5, midy-2, 5, 5);
 
381
    }
 
382
  }
 
383
 
 
384
  /**
 
385
   * Overides default read object in order to reload icons.
 
386
   * This is necessary because for some strange reason animated
 
387
   * gifs stop being animated after being serialized/deserialized.
 
388
   *
 
389
   * @param ois an <code>ObjectInputStream</code> value
 
390
   * @exception IOException if an error occurs
 
391
   * @exception ClassNotFoundException if an error occurs
 
392
   */
 
393
  private void readObject(ObjectInputStream ois) 
 
394
    throws IOException, ClassNotFoundException {
 
395
    try {
 
396
      ois.defaultReadObject();
 
397
      remove(m_visualLabel);
 
398
      m_visualLabel = new JLabel(m_icon);
 
399
      loadIcons(m_iconPath, m_animatedIconPath);
 
400
      add(m_visualLabel, BorderLayout.CENTER);
 
401
      Dimension d = m_visualLabel.getPreferredSize();
 
402
      Dimension d2 = new Dimension((int)d.getWidth() + 10, 
 
403
                                   (int)d.getHeight() + 10);
 
404
      setMinimumSize(d2);
 
405
      setPreferredSize(d2);
 
406
      setMaximumSize(d2);   
 
407
    } catch (Exception ex) {
 
408
      ex.printStackTrace();
 
409
    }
 
410
  }
 
411
}