1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.drawing; |
16 | |
17 | import java.awt.image.BufferedImage; |
18 | |
19 | import javax.swing.JButton; |
20 | import javax.swing.JComponent; |
21 | import javax.swing.JFileChooser; |
22 | |
23 | import org.jdesktop.application.ApplicationContext; |
24 | |
25 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
26 | import net.sourceforge.hivegui.component.ExtensionsFileFilter; |
27 | import net.sourceforge.hivegui.dialog.DialogPanel; |
28 | import net.sourceforge.hivegui.dialog.GenericDialog; |
29 | import net.sourceforge.hivegui.imaging.ImageIOHelper; |
30 | |
31 | /** |
32 | * @author Jean-Francois Poilpret |
33 | */ |
34 | public class ImageToolMainPanel extends JFileChooser implements DialogPanel |
35 | { |
36 | public ImageToolMainPanel(ImageCompositor compositor) |
37 | { |
38 | super(); |
39 | setName("image-tool"); |
40 | |
41 | // Set UI settings for dialogs |
42 | setDialogTitle("Open Image"); |
43 | setApproveButtonText("Insert Image"); |
44 | setApproveButtonMnemonic('I'); |
45 | |
46 | _settings = new ImageToolSettingsPanel(compositor, true); |
47 | setAccessory(_settings); |
48 | addPropertyChangeListener(SELECTED_FILE_CHANGED_PROPERTY, _settings); |
49 | // Set list of filters based on acceptable extensions |
50 | String[] extensions = ImageIOHelper.getReaderExtensions(); |
51 | resetChoosableFileFilters(); |
52 | addChoosableFileFilter(new ExtensionsFileFilter(extensions)); |
53 | } |
54 | |
55 | public void setSourceImage(BufferedImage image) |
56 | { |
57 | _settings.setSourceImage(image); |
58 | // Reset previous selection, only keep previous directory |
59 | setSelectedFile(null); |
60 | } |
61 | |
62 | public void reset() |
63 | { |
64 | // Do nothing (because everything is set before this method is called) |
65 | } |
66 | |
67 | public void forceReset() |
68 | { |
69 | _settings.reset(); |
70 | } |
71 | |
72 | public ImageToolSettings getUserSettings() |
73 | { |
74 | return _settings.getUserSettings(); |
75 | } |
76 | |
77 | @Override public void approveSelection() |
78 | { |
79 | super.approveSelection(); |
80 | _dialog.close(false); |
81 | } |
82 | |
83 | @Override public void cancelSelection() |
84 | { |
85 | super.cancelSelection(); |
86 | _dialog.close(true); |
87 | } |
88 | |
89 | public boolean canClose() |
90 | { |
91 | return true; |
92 | } |
93 | |
94 | public JButton getDefaultButton() |
95 | { |
96 | return null; |
97 | } |
98 | |
99 | public JComponent getPanel() |
100 | { |
101 | return this; |
102 | } |
103 | |
104 | public void setContext(ApplicationContext context) |
105 | { |
106 | _settings.setApplication((HiveGuiApplicationMain) context.getApplication()); |
107 | } |
108 | |
109 | public void setParent(GenericDialog dialog) |
110 | { |
111 | _dialog = dialog; |
112 | } |
113 | |
114 | // Fix for Java bug #6317789 (sporadic hang in JFileChooser on Windows) |
115 | @Override public void updateUI() |
116 | { |
117 | putClientProperty("FileChooser.useShellFolder", Boolean.FALSE); |
118 | super.updateUI(); |
119 | } |
120 | |
121 | private ImageToolSettingsPanel _settings; |
122 | private GenericDialog _dialog; |
123 | } |