File: JiuAwtFrame.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,304 kB
  • ctags: 3,737
  • sloc: java: 31,190; sh: 238; xml: 30; makefile: 19
file content (377 lines) | stat: -rw-r--r-- 10,092 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * JiuAwtFrame
 * 
 * Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.gui.awt;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Label;
import java.awt.ScrollPane;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import net.sourceforge.jiu.apps.EditorState;
import net.sourceforge.jiu.apps.ImageDescriptionCreator;
import net.sourceforge.jiu.apps.JiuInfo;
import net.sourceforge.jiu.apps.StringIndexConstants;
import net.sourceforge.jiu.apps.Strings;
import net.sourceforge.jiu.data.PixelImage;
import net.sourceforge.jiu.gui.awt.AwtMenuWrapper;
import net.sourceforge.jiu.gui.awt.ImageCanvas;
import net.sourceforge.jiu.gui.awt.dialogs.InfoDialog;
import net.sourceforge.jiu.ops.ProgressListener;

/**
 * The frame class for the AWT demo program {@link net.sourceforge.jiu.apps.jiuawt}.
 * @author Marco Schmidt
 * @since 0.8.0
 */
public class JiuAwtFrame extends Frame implements ActionListener, ComponentListener, JiuInfo, ProgressListener
{
	/**
	 * The name of this application, jiuawt, plus the version number taken
	 * from {@link JiuInfo}.
	 * Example: <code>jiuawt 0.8.0</code>.
	 * Will be displayed in the title bar of this frame.
	 */
	public static final String APP_NAME = "jiuawt " + JiuInfo.JIU_VERSION;
	static final long serialVersionUID = 2592450425245L;
	private EditorState editor;
	private AwtMenuWrapper menuWrapper;
	private AwtOperationProcessor processor;
	private Label statusBar;
	private ScrollPane scrollPane;
	private ImageCanvas canvas;

	/**
	 * Create an object of this class, using the argument editor
	 * state.
	 * String resources to initialize the menu etc. will be taken
	 * from the EditorState object's Strings variable
	 * @param editorState EditorState object used by this frame
	 */
	public JiuAwtFrame(EditorState editorState)
	{
		super(APP_NAME);
		processor = new AwtOperationProcessor(editorState, this);
		editor = editorState;
		editor.addProgressListener(this);
		addComponentListener(this);
		addWindowListener(new WindowAdapter()
		{
      		public void windowClosing(WindowEvent e)
      		{
        		processor.fileExit();
      		}
    	});
		// MENU
		menuWrapper = new AwtMenuWrapper(editor.getStrings(), this);
		setMenuBar(menuWrapper.getMenuBar());
		menuWrapper.updateEnabled(processor);
		// IMAGE CANVAS
		// STATUS BAR
		statusBar = new Label("");
		add(statusBar, BorderLayout.SOUTH);
		maximize();
		//pack();
		repaint();
		setVisible(true);
		if (editor.getStartupImageName() != null)
		{
			processor.fileOpen(null);
		}
	}

	/**
	 * Processes event objects that get created when menu items are
	 * picked.
	 * Determines the {@link net.sourceforge.jiu.apps.MenuIndexConstants} value for a given
	 * event object and calls the internal {@link AwtOperationProcessor}
	 * object's process method with the menu value.
	 * The operation will then be performed.
	 * @param e the ActionEvent object
	 */
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		int index = menuWrapper.findIndex(source);
		if (index != -1)
		{
			processor.process(index);
		}
	}

	public void componentHidden(ComponentEvent e)
	{
	}

	public void componentMoved(ComponentEvent e)
	{
	}

	public void componentResized(ComponentEvent e)
	{
		if (scrollPane != null)
		{
			canvas.computeZoomToFitSize();
			scrollPane.doLayout();
		}
	}

	public void componentShown(ComponentEvent e)
	{
	}

	/**
	 * Maximize the frame on the desktop.
	 * There is no such function in the 1.1 AWT (was added in 1.4), so
	 * this class determines the screen size and sets the frame to be 
	 * a little smaller than that (to make up for task bars etc.).
	 * So this is just a heuristical approach.
	 */
	public void maximize()
	{
		/*
		The following line:
		setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
		does a nice maximization, but works only with Java 1.4+
		*/
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		if (toolkit == null)
		{
			return;
		}
		Dimension screenSize = toolkit.getScreenSize();
		if (screenSize == null)
		{
			return;
		}
		int w = screenSize.width;
		int h = screenSize.height;
		int x = 20;
		int y = 80;
		setLocation(x / 2, y / 2);
		setSize(w - x, h - y);
	}

	/**
	 * Displays the argument text in a message box with
	 * error in the title bar.
	 * @param text the error message to be displayed
	 */
	public void showError(String text)
	{
		Strings strings = editor.getStrings();
		showInfo(strings.get(StringIndexConstants.ERROR_MESSAGE), text);
	}

	/**
	 * Sets the current cursor to be {@link java.awt.Cursor#DEFAULT_CURSOR}.
	 */
	public void setDefaultCursor()
	{
		Cursor cursor = new Cursor(Cursor.DEFAULT_CURSOR);
		setCursor(cursor);
	}

	/**
	 * If an image is currently loaded,
	 */
	public void setOriginalSize()
	{
		if (canvas != null && !editor.isZoomOriginalSize())
		{
			editor.zoomSetOriginalSize();
			canvas.setZoomFactors(editor.getZoomFactorX(), editor.getZoomFactorY());
			updateTitle();
			menuWrapper.updateEnabled(processor);
		}
	}

	public void setProgress(int zeroBasedIndex, int totalItems)
	{
		if (totalItems < 1)
		{
			throw new IllegalArgumentException("Total number of items (second parameter) must be larger than zero.");
		}
		if (zeroBasedIndex < 0)
		{
			throw new IllegalArgumentException("Zero-based index must be at least zero.");
		}
		if (zeroBasedIndex >= totalItems)
		{
			throw new IllegalArgumentException("Zero-based index must be smaller than total " +
				"number of items; zeroBasedIndex=" + zeroBasedIndex + ", totalItems=" +
				totalItems);
		}
		setProgress((float)(zeroBasedIndex + 1) / (float)totalItems);
	}

	/**
	 * Set a new progress status.
	 * @param progress float from 0.0f to 1.0f, indicating the progress between 0 and 100 percent 
	 */
	public void setProgress(float progress)
	{
		if (progress >= 0.0f && progress <= 1.0f)
		{
			setStatusBar(" " + Math.round(progress * 100.0f) + "%");
		}
	}

	public void setStatusBar(String text)
	{
		statusBar.setText(text);
	}

	public void setWaitCursor()
	{
		Cursor cursor = new Cursor(Cursor.WAIT_CURSOR);
		setCursor(cursor);
	}

	/**
	 * Shows a modal dialog with given title bar and message text.
	 * @param title will be displayed in the dialog's title bar
	 * @param text will be displayed in the dialog's center part
	 */
	public void showInfo(String title, String text)
	{
		InfoDialog d = new InfoDialog(this, title, text);
		d.setVisible(true);
	}

	/**
	 * If there is an image loaded, forces a canvas redraw by 
	 * calling repaint.
	 */
	public void updateCanvas()
	{
		if (canvas != null)
		{
			canvas.setInterpolation(editor.getInterpolation());
			//canvas.revalidate();
			canvas.repaint();
		}
	}

	/**
	 * Removes the current canvas from the frame (if there
	 * is an image loaded) and creates a new canvas for the
	 * current image.
	 */
	public void updateImage()
	{
		PixelImage image = editor.getImage();
		if (scrollPane != null)
		{
			remove(scrollPane);
		}
		if (image != null)
		{
			//editor.zoomSetOriginalSize();
			Image awtImage = ImageCreator.convertToAwtImage(image, RGBA.DEFAULT_ALPHA);
			scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
			canvas = new ImageCanvas(scrollPane);
			canvas.setInterpolation(editor.getInterpolation());
			canvas.setZoomToFit(editor.getZoomToFit());
			canvas.setImage(awtImage);
			canvas.setZoomFactors(editor.getZoomFactorX(), editor.getZoomFactorY());
			//canvas.computeZoomToFitSize();
			scrollPane.add(canvas);
			add(scrollPane);
		}
		updateStatusBar();
		updateTitle();
		validate();
		menuWrapper.updateEnabled(processor);
	}

	/**
	 * Creates a description string for the current image and sets the
	 * status bar to that text.
	 */
	public void updateStatusBar()
	{
		PixelImage image = editor.getImage();
		String statusBarText;
		if (image == null)
		{
			statusBarText = "";
		}
		else
		{
			statusBarText = ImageDescriptionCreator.getDescription(image, editor.getLocale(), editor.getStrings());
		}
		setStatusBar(statusBarText);
	}

	/**
	 * Sets the frame's title bar to the application name, plus the file name of
	 * the currently loaded image file, plus the current zoom factor, plus an
	 * optional asterisk in case the image was modified but not yet saved.
	 */
	public void updateTitle()
	{
		StringBuffer sb = new StringBuffer(APP_NAME);
		String fileName = editor.getFileName();
		if (fileName != null && fileName.length() > 0)
		{
			sb.append(" [");
			sb.append(fileName);
			if (editor.getModified())
			{
				sb.append('*');
			}
			sb.append(']');
		}
		if (editor.getImage() != null)
		{
			double zoom = editor.getZoomFactorX();
			int percent = (int)(zoom * 100.0);
			sb.append(' ');
			sb.append(Integer.toString(percent));
			sb.append('%');
		}
		setTitle(sb.toString());
	}

	/**
	 * If an image is currently displayed, zoom in one level.
	 */
	public void zoomIn()
	{
		if (canvas != null && !editor.isMaximumZoom())
		{
			editor.zoomIn();
			canvas.setZoomFactors(editor.getZoomFactorX(), editor.getZoomFactorY());
			updateTitle();
			menuWrapper.updateEnabled(processor);
		}
	}

	/**
	 * If an image is currently displayed, zoom out one level.
	 */
	public void zoomOut()
	{
		if (canvas != null && !editor.isMinimumZoom())
		{
			editor.zoomOut();
			canvas.setZoomFactors(editor.getZoomFactorX(), editor.getZoomFactorY());
			updateTitle();
			menuWrapper.updateEnabled(processor);
		}
	}
}