Source code: org/modama/gui/viewer/ImageViewerOptions.java
1 /**
2 * Created by IntelliJ IDEA.
3 * User: Administrator
4 * Date: Apr 10, 2003
5 * Time: 12:13:18 AM
6 * To change this template use Options | File Templates.
7 */
8 package org.modama.gui.viewer;
9
10 import beantest.property.PropertyPane;
11
12 import javax.swing.*;
13 import javax.media.jai.JAI;
14 import java.awt.*;
15 import java.awt.event.ActionListener;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ComponentAdapter;
18 import java.awt.event.ComponentEvent;
19 import java.awt.image.RenderedImage;
20 import java.awt.image.BufferedImage;
21 import java.beans.PropertyChangeListener;
22 import java.beans.PropertyChangeEvent;
23
24 import com.brunchboy.util.swing.relativelayout.RelativeLayout;
25 import com.brunchboy.util.swing.relativelayout.AttributeType;
26 import com.brunchboy.util.swing.relativelayout.AttributeConstraint;
27 import com.brunchboy.util.swing.relativelayout.DependencyManager;
28 import com.sun.media.jai.widget.DisplayJAI;
29 import org.modama.framework.tools.Resources;
30 import org.modama.gui.viewer.rule.RuleFactory;
31 import org.modama.gui.viewer.rule.RulePropertys;
32
33 /**
34 * a class for editing the propertys of a CoordinateImageViewer<br>
35 * at the moment their are panels for:
36 * general -> viewport, axes on/off, lutviewer on/off
37 * rulepropertys -> the props for creating the rules
38 *
39 * TODO die ganze klasse ist scheise und buggi (beim viewport), und es bleibt die frage was man das mit dem updaten handhabt
40 */
41 public class ImageViewerOptions extends JTabbedPane {
42 /**
43 * the viewer
44 */
45 CoordinateImageViewer viewer;
46
47 /******************************************
48 * the panels
49 */
50 /**
51 * panel showing following options:
52 * the viewport
53 * zoom -> procent
54 * viewposition -> x,y
55 * a thumbnail with a rectangle showing the viewport
56 * checkboxes for showing axes, lutviewer
57 * checkbox for showing scale of lutviewer
58 */
59 class GeneralPanel extends JPanel {
60 JPanel viewport;
61 JTextField zoom,posx,posy;
62 //DisplayJAI thumbnail;
63 JComponent thumbnail;
64 JCheckBox axes,lut,scale;
65 RenderedImage thumbnailimage;
66
67 public GeneralPanel() {
68 setupGUI();
69 setupEvents();
70 setupValues();
71 }
72
73 private void setupValues()
74 {
75 zoom.setText( Double.toString( viewer.getScalex() * 100) );
76 posx.setText( Double.toString( viewer.getPosx() ) );
77 posy.setText( Double.toString( viewer.getPosy() ) );
78 axes.setSelected( viewer.isEnabledrules() );
79 // lut.setSelected( viewer.isEnabledLUTViewer() );
80 // scale.setSelected( viewer.getLUTViewer().isEnabledscale() );
81
82 }
83
84 private void setupEvents()
85 {
86 // checkboxes
87 axes.addActionListener( new ActionListener() {
88 public void actionPerformed(ActionEvent e) {
89 viewer.setEnabledrules( axes.isSelected() );
90 }
91 });
92 /*
93 lut.addActionListener( new ActionListener() {
94 public void actionPerformed(ActionEvent e) {
95 viewer.setEnabledLUTViewer( lut.isSelected() );
96 }
97 });
98 scale.addActionListener( new ActionListener() {
99 public void actionPerformed(ActionEvent e) {
100 viewer.getLUTViewer().setEnabledscale( scale.isSelected() );
101 }
102 });
103 */
104 // texfields
105 zoom.addActionListener( new ActionListener() {
106 public void actionPerformed(ActionEvent e) {
107 try{
108 double z = Double.parseDouble( zoom.getText() );
109 // check for range
110 if( z > 800 ) z = 800;
111 if( z < 1 ) z = 1;
112 z /= 100;
113 viewer.setScale( z, z );
114 zoom.setText( Double.toString(z*100) );
115 } catch( NumberFormatException ex ) {}
116 }
117 });
118 //pos
119 posx.addActionListener( new ActionListener() {
120 public void actionPerformed(ActionEvent e) {
121 try{
122 int z = Integer.parseInt( posx.getText() );
123 Point p = viewer.getViewPosition();
124 p.x = -z;
125 viewer.setViewPosition( p );
126 posx.setText( Integer.toString( -viewer.getPosx() ) );
127 } catch( NumberFormatException ex ) {}
128 }
129 });
130 posy.addActionListener( new ActionListener() {
131 public void actionPerformed(ActionEvent e) {
132 try{
133 int z = Integer.parseInt( posy.getText() );
134 Point p = viewer.getViewPosition();
135 p.y = -z;
136 viewer.setViewPosition( p );
137 posy.setText( Integer.toString( -viewer.getPosy() ) );
138 } catch( NumberFormatException ex ) {}
139 }
140 });
141 }
142
143 private void setupGUI() {
144 // layoutmanager
145 RelativeLayout layout = new RelativeLayout();
146 setLayout( layout );
147 // create objects
148 createViewportPanel();
149 axes = new JCheckBox( Resources.getString("iviewer.showaxes"));
150 lut = new JCheckBox( Resources.getString("iviewer.showlut"));
151 scale = new JCheckBox( Resources.getString("iviewer.showscale"));
152 // set preferred size
153 setPreferredSize( new Dimension( 300,300 ));
154 // add the components
155 add( "viewport",viewport );
156 add( "axes",axes );
157 add( "lut",lut );
158 add( "scale",scale );
159 // set layout
160 // viewport
161 layout.addConstraint( "viewport", AttributeType.TOP, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.TOP, 10 ));
162 layout.addConstraint( "viewport", AttributeType.LEFT, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.LEFT, 10 ));
163 layout.addConstraint( "viewport", AttributeType.RIGHT, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.RIGHT, -10 ));
164 // axes
165 layout.addConstraint( "axes", AttributeType.TOP, new AttributeConstraint( "viewport", AttributeType.BOTTOM, 10 ));
166 layout.addConstraint( "axes", AttributeType.LEFT, new AttributeConstraint( "viewport", AttributeType.LEFT ));
167 // lut
168 layout.addConstraint( "lut", AttributeType.TOP, new AttributeConstraint( "axes", AttributeType.BOTTOM, 10 ));
169 layout.addConstraint( "lut", AttributeType.LEFT, new AttributeConstraint( "viewport", AttributeType.LEFT ));
170 // scale
171 layout.addConstraint( "scale", AttributeType.TOP, new AttributeConstraint( "lut", AttributeType.BOTTOM ));
172 layout.addConstraint( "scale", AttributeType.LEFT, new AttributeConstraint( "lut", AttributeType.LEFT, 20 ));
173 }
174
175 protected void createViewportPanel()
176 {
177 // layoutmanager
178 RelativeLayout layout = new RelativeLayout();
179 // create objects
180 viewport = new JPanel( layout );
181 zoom = new JTextField();
182 posx = new JTextField();
183 posy = new JTextField();
184 createThumbnaildisplay();
185 // set preferredsizes
186 zoom.setPreferredSize( new Dimension( 40, 20 ));
187 posx.setPreferredSize( new Dimension( 40, 20 ));
188 posy.setPreferredSize( new Dimension( 40, 20 ));
189 // labels
190 JLabel zoomlabel = new JLabel( Resources.getString("iviewer.zoom") );
191 JLabel poslabel = new JLabel( Resources.getString("iviewer.viewpos") );
192 JLabel procent = new JLabel( "%" );
193 JLabel x = new JLabel("X");
194 JLabel y = new JLabel("Y");
195 // border of the panel
196 viewport.setBorder( BorderFactory.createTitledBorder( Resources.getString("iviewer.viewport") ) );
197 // add the components
198 viewport.add( "zoomlabel", zoomlabel );
199 viewport.add( "zoom", zoom );
200 viewport.add( "procent", procent );
201 viewport.add( "poslabel", poslabel );
202 viewport.add( "posx", posx );
203 viewport.add( "posy", posy );
204 viewport.add( "thumbnail", thumbnail );
205 viewport.add( "x", x );
206 viewport.add( "y", y );
207 // set the layout
208 // Zoomlabel in the upper left corner
209 layout.addConstraint( "zoomlabel", AttributeType.TOP, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.TOP, 10 ));
210 layout.addConstraint( "zoomlabel", AttributeType.LEFT, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.LEFT, 10 ));
211 // viewposlabel below the zoomlabel
212 layout.addConstraint( "poslabel", AttributeType.TOP, new AttributeConstraint( "zoomlabel", AttributeType.BOTTOM, 10 ));
213 layout.addConstraint( "poslabel", AttributeType.LEFT, new AttributeConstraint( "zoomlabel", AttributeType.LEFT ));
214 // zoom textfiel to the right fo the labels
215 layout.addConstraint( "zoom", AttributeType.TOP , new AttributeConstraint( "zoomlabel", AttributeType.TOP ));
216 layout.addConstraint( "zoom", AttributeType.LEFT , new AttributeConstraint( "zoomlabel,poslabel", AttributeType.RIGHT, 20 ));
217 // width of the zoomfiel
218 //layout.addConstraint( "zoom", AttributeType.WIDTH , new AttributeConstraint( null, null, 100 ));
219 // procentlabel to the right of the zoomfield
220 layout.addConstraint( "procent", AttributeType.TOP , new AttributeConstraint( "zoom", AttributeType.TOP ));
221 layout.addConstraint( "procent", AttributeType.LEFT , new AttributeConstraint( "zoom", AttributeType.RIGHT , 5 ));
222 // x label
223 layout.addConstraint( "x", AttributeType.TOP, new AttributeConstraint( "poslabel", AttributeType.TOP ));
224 layout.addConstraint( "x", AttributeType.LEFT, new AttributeConstraint( "zoom", AttributeType.LEFT ));
225 // y label
226 layout.addConstraint( "y", AttributeType.TOP, new AttributeConstraint( "x", AttributeType.BOTTOM, 10 ));
227 layout.addConstraint( "y", AttributeType.LEFT, new AttributeConstraint( "x", AttributeType.LEFT ));
228 // posx
229 layout.addConstraint( "posx", AttributeType.TOP, new AttributeConstraint( "x", AttributeType.TOP ));
230 layout.addConstraint( "posx", AttributeType.LEFT, new AttributeConstraint( "x", AttributeType.RIGHT, 10 ));
231 // posy
232 layout.addConstraint( "posy", AttributeType.TOP, new AttributeConstraint( "y", AttributeType.TOP ));
233 layout.addConstraint( "posy", AttributeType.LEFT, new AttributeConstraint( "x", AttributeType.RIGHT, 10 ));
234 // thumbnail, the rest of the space
235 layout.addConstraint( "thumbnail", AttributeType.TOP, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.TOP, 10 ));
236 //layout.addConstraint( "thumbnail", AttributeType.BOTTOM, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.BOTTOM, -10 ));
237 layout.addConstraint( "thumbnail", AttributeType.RIGHT, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.RIGHT, -10 ));
238 layout.addConstraint( "thumbnail", AttributeType.LEFT , new AttributeConstraint( "procent,posx,posy", AttributeType.RIGHT, 20 ));
239 }
240
241 protected void createThumbnaildisplay()
242 {
243 thumbnailimage = viewer.getThumbnail();
244 if( thumbnailimage == null ) {
245 thumbnail = new JLabel("No Image");
246 return;
247 }
248 // TODO add posibility to click in the thumbnail
249 thumbnail = new DisplayJAI( thumbnailimage ) {
250 int width, height;
251 public synchronized void paintComponent(Graphics graphics) {
252 super.paintComponent(graphics);
253 // get the rect
254 Point p = viewer.getViewPosition();
255 p.x = -p.x;
256 p.y = -p.y;
257 double x = (double)viewer.getViewer().getWidth() / (viewer.getViewer().getProcessedImage().getWidth() * viewer.getScalex());
258 double y = (double)viewer.getViewer().getHeight() / (viewer.getViewer().getProcessedImage().getHeight() * viewer.getScaley());
259 width = (int)(x * thumbnailimage.getWidth());
260 height = (int)(y * thumbnailimage.getHeight());
261 p.x *= x;
262 p.y *= y;
263 if( p.x >= thumbnailimage.getWidth() || p.y >= thumbnailimage.getHeight() ) return;
264 if( p.x + width > thumbnailimage.getWidth() ) width = thumbnailimage.getWidth() - p.x;
265 if( p.y + height > thumbnailimage.getHeight() ) height = thumbnailimage.getHeight() - p.y - 1;
266 // draw the rect
267 graphics.setColor( Color.RED );
268 graphics.drawRect( p.x, p.y, width, height );
269 }
270 };
271 }
272 }
273
274 class AxePanel extends JPanel
275 {
276 BufferedImage rule;
277 RuleFactory rulefactory;
278 RulePropertys propertys;
279 DisplayJAI ruledisplay;
280 JComboBox cbox;
281 PropertyPane pp;
282 Repainter repainter;
283
284 class Repainter implements PropertyChangeListener {
285 public void propertyChange(PropertyChangeEvent evt) {
286 createRule();
287 repaint();
288 }
289 }
290
291 public AxePanel() {
292 rule = null;
293 rulefactory = new RuleFactory( viewer.getHorizontalprops(), 0, 100, getWidth() - 20 );
294 repainter = new Repainter();
295 setupGUI();
296 setupEvents();
297 setProps( viewer.getHorizontalprops() );
298 }
299
300 private void setupGUI()
301 {
302 // layoutmanager
303 RelativeLayout layout = new RelativeLayout();
304 setLayout( layout );
305 // create objects
306 cbox = new JComboBox( new String[] {"X Axe","Y Axe","Lookuptable-scaling"} );
307 ruledisplay = new DisplayJAI();
308 pp = new PropertyPane();
309 pp.setBean( viewer.getHorizontalprops() );
310 // add objects
311 add( "cbox", cbox);
312 add( "ruledisplay", ruledisplay );
313 add( "pp", pp );
314 // layout
315 // cbox
316 layout.addConstraint( "cbox", AttributeType.TOP, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.TOP, 10 ));
317 layout.addConstraint( "cbox", AttributeType.LEFT, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.LEFT, 10 ));
318 layout.addConstraint( "cbox", AttributeType.RIGHT, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.RIGHT, -10 ));
319 // ruledisplay
320 layout.addConstraint( "ruledisplay", AttributeType.TOP, new AttributeConstraint( "cbox", AttributeType.BOTTOM, 10 ));
321 layout.addConstraint( "ruledisplay", AttributeType.LEFT, new AttributeConstraint( "cbox", AttributeType.LEFT ));
322 // pp
323 layout.addConstraint( "pp", AttributeType.TOP, new AttributeConstraint( "ruledisplay", AttributeType.BOTTOM, 10 ));
324 layout.addConstraint( "pp", AttributeType.LEFT, new AttributeConstraint( "cbox", AttributeType.LEFT ));
325 layout.addConstraint( "pp", AttributeType.RIGHT, new AttributeConstraint( "cbox", AttributeType.RIGHT ));
326 layout.addConstraint( "pp", AttributeType.BOTTOM, new AttributeConstraint( DependencyManager.ROOT_NAME, AttributeType.BOTTOM, -10 ));
327 }
328
329 private void createRule()
330 {
331 rulefactory.setSize( getWidth() - 20 );
332 rule = rulefactory.create( RuleFactory.HORIZONTAL_TOP );
333 ruledisplay.set( rule );
334 }
335
336 private void setProps( RulePropertys pro )
337 {
338 if( pro != propertys )
339 {
340 if( propertys != null ) propertys.removePropertyChangeListener( repainter );
341 pro.addPropertyChangeListener( repainter );
342 pp.setBean( pro );
343 rulefactory.setProps( pro );
344 }
345 }
346
347 private void setupEvents()
348 {
349 addComponentListener( new ComponentAdapter() {
350 public void componentResized(ComponentEvent e) {
351 createRule();
352 }
353 });
354 cbox.addActionListener( new ActionListener() {
355 public void actionPerformed(ActionEvent e) {
356 switch( cbox.getSelectedIndex() ){
357 case 0 : setProps( viewer.getHorizontalprops() );break;
358 case 1 : setProps( viewer.getVerticalprops() );break;
359 // case 2 : setProps( viewer.getLUTViewer().getRulePropertys() );break;
360 }
361 createRule();
362 repaint();
363 }
364 });
365 }
366 }
367
368 public ImageViewerOptions(CoordinateImageViewer viewer) {
369 this.viewer = viewer;
370 setupGUI();
371 setupEvents();
372 }
373
374 protected void setupGUI()
375 {
376 add( "General", new GeneralPanel() );
377 add( "Axes", new AxePanel() );
378 }
379
380 protected void setupEvents()
381 {
382
383 }
384
385 public static void main(String[] args) {
386 JFrame fr = new JFrame();
387 JFrame fr2 = new JFrame();
388
389 RenderedImage img = JAI.create( "fileload", "c:\\smallbabies.jpeg" );
390
391 CoordinateImageViewer viewer = new CoordinateImageViewer( img );
392
393 fr2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
394 fr2.getContentPane().setLayout( new BorderLayout() );
395 fr2.getContentPane().add( viewer );
396 fr2.pack();
397 fr2.show();
398
399 ImageViewerOptions ivo = new ImageViewerOptions( viewer );
400
401 fr.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
402 fr.getContentPane().setLayout( new BorderLayout() );
403 fr.getContentPane().add( ivo );
404
405 //fr.setSize( 300,300);
406 fr.pack();
407 fr.show();
408
409 }
410 }