1 /* Copyright (C) 2005-2011 Fabio Riccardi */
2 
3 package com.lightcrafts.app;
4 
5 import com.lightcrafts.platform.AlertDialog;
6 import com.lightcrafts.platform.FileChooser;
7 import com.lightcrafts.platform.Platform;
8 import com.lightcrafts.ui.toolkit.TextAreaFactory;
9 
10 import static com.lightcrafts.app.Locale.LOCALE;
11 import com.lightcrafts.image.ImageFilenameFilter;
12 
13 import javax.swing.*;
14 import javax.swing.event.ListSelectionEvent;
15 import javax.swing.event.ListSelectionListener;
16 import java.awt.*;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.io.File;
20 
21 // Show dialogs to get user guidance about how to find the image file for a
22 // Document, in case the Document's file pointers aren't valid.
23 
24 class DocumentImageSelector {
25 
26     // For file choosers:
27     private static Platform Env = Platform.getPlatform();
28 
29     // The object of all the choosers:
30     private static File file;
31 
32     // Pick an image file from among an array of alternatives, or move on
33     // to a file chooser.  If the user cancels, return null.
chooseImageFile( File docFile, final File oldImageFile, File[] imageFiles, final File chooserFile, final Frame parent )34     static File chooseImageFile(
35         File docFile,
36         final File oldImageFile,
37         File[] imageFiles,
38         final File chooserFile,
39         final Frame parent
40     ) {
41         if (imageFiles.length == 0) {
42             return chooseImageFile(docFile, oldImageFile, chooserFile, parent);
43         }
44         if (imageFiles.length == 1) {
45             return chooseImageFile(
46                 docFile, oldImageFile, imageFiles[0], chooserFile, parent
47             );
48         }
49         // It's definitely multiple-choice.
50         String bigText = LOCALE.get(
51             "ImageSelectorMessage1Major",
52             docFile.getName(),
53             oldImageFile.getAbsolutePath()
54         );
55         String smallText = LOCALE.get("ImageSelectorMessage1Minor");
56 
57         JTextArea bigTextComp = TextAreaFactory.createTextArea(bigText, 30);
58         bigTextComp.setFont(bigTextComp.getFont().deriveFont(Font.BOLD));
59         JTextArea smallTextComp = TextAreaFactory.createTextArea(smallText, 30);
60 
61         Object[] listEntries = new Object[imageFiles.length];
62         for (int n=0; n<imageFiles.length; n++) {
63             // An anonymous inner class to control JList rendering:
64             File file = new File(imageFiles[n], "") {
65                 public String toString() {
66                     return getAbsolutePath();
67                 }
68             };
69             listEntries[n] = file;
70         }
71         final JList list = new JList(listEntries);
72         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
73         list.setBorder(BorderFactory.createLineBorder(Color.black));
74 
75         final JButton useThis =
76             new JButton(LOCALE.get("ImageSelector1UseOption"));
77         final JButton search =
78             new JButton(LOCALE.get("ImageSelectorSearchOption"));
79         final JButton cancel =
80             new JButton(LOCALE.get("ImageSelectorCancelOption"));
81 
82         list.addListSelectionListener(
83             new ListSelectionListener() {
84                 public void valueChanged(ListSelectionEvent event) {
85                     Object selection = list.getSelectedValue();
86                     useThis.setEnabled(selection != null);
87                 }
88             }
89         );
90         useThis.setEnabled(false);
91 
92         final String findTitle = LOCALE.get(
93             "ImageSelectorFindDialogTitle", oldImageFile.getName()
94         );
95         final JDialog dialog = new JDialog(parent, findTitle, true);
96         dialog.setResizable(false);
97 
98         useThis.addActionListener(
99             new ActionListener() {
100                 public void actionPerformed(ActionEvent event) {
101                     file = (File) list.getSelectedValue();
102                     dialog.dispose();
103                 }
104             }
105         );
106         search.addActionListener(
107             new ActionListener() {
108                 public void actionPerformed(ActionEvent event) {
109                     FileChooser chooser = Env.getFileChooser();
110                     file = chooser.openFile(
111                         findTitle, chooserFile, parent,
112                         ImageFilenameFilter.INSTANCE
113                     );
114                     if (file != null) {
115                         dialog.dispose();
116                     }
117                 }
118             }
119         );
120         cancel.addActionListener(
121             new ActionListener() {
122                 public void actionPerformed(ActionEvent event) {
123                     file = null;
124                     dialog.dispose();
125                 }
126             }
127         );
128         JPanel contents = new JPanel();
129         contents.setLayout(new BoxLayout(contents, BoxLayout.Y_AXIS));
130         contents.add(bigTextComp);
131         contents.add(Box.createVerticalStrut(6));
132         contents.add(smallTextComp);
133         contents.add(Box.createVerticalStrut(6));
134         contents.add(list);
135         contents.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
136 
137         bigTextComp.setBackground(contents.getBackground());
138         smallTextComp.setBackground(contents.getBackground());
139 
140         JOptionPane option =
141             new JOptionPane(contents, JOptionPane.QUESTION_MESSAGE);
142         option.setOptions(new Object[] {cancel, search, useThis});
143 
144         dialog.add(option);
145         dialog.pack();
146         dialog.setLocationRelativeTo(parent);
147         dialog.setVisible(true);
148 
149         return file;
150     }
151 
152     // Ask if a given image file is OK, and if it's not, then move on to a
153     // file chooser.
chooseImageFile( File docFile, File oldImageFile, File newImageFile, File chooserFile, Frame parent )154     static File chooseImageFile(
155         File docFile,
156         File oldImageFile,
157         File newImageFile,
158         File chooserFile,
159         Frame parent
160     ) {
161         // Is this the right one?
162         int result = Env.getAlertDialog().showAlert(
163             parent,
164             LOCALE.get(
165                 "ImageSelectorMessage2Major",
166                 docFile.getName(),
167                 oldImageFile.getName(),
168                 newImageFile.getAbsolutePath()
169             ),
170             LOCALE.get("ImageSelectorMessage2Minor"),
171             AlertDialog.ERROR_ALERT,
172             LOCALE.get("ImageSelector2UseOption", newImageFile.getName()),
173             LOCALE.get("ImageSelectorSearchOption"),
174             LOCALE.get("ImageSelectorCancelOption")
175         );
176         if (result == 0) {  // Use this image
177             file = newImageFile;
178         }
179         else if (result == 1) { // Search for the image
180             FileChooser chooser = Env.getFileChooser();
181             String title = LOCALE.get(
182                 "ImageSelectorFindDialogTitle", oldImageFile.getName()
183             );
184             file = chooser.openFile(
185                 title, chooserFile, parent, ImageFilenameFilter.INSTANCE
186             );
187         }
188         else {
189             file = null;    // Cancel
190         }
191         return file;
192     }
193 
194     // Pick an image file in a file chooser.
chooseImageFile( File docFile, File oldImageFile, File chooserFile, Frame parent)195     static File chooseImageFile(
196         File docFile,
197         File oldImageFile,
198         File chooserFile,
199         Frame parent) {
200         // Would you like to search for it?
201         int result = Env.getAlertDialog().showAlert(
202             parent,
203             LOCALE.get(
204                 "ImageSelectorMessage3Major",
205                 docFile.getName(),
206                 oldImageFile.getName()
207             ),
208             LOCALE.get("ImageSelectorMessage3Minor"),
209             AlertDialog.ERROR_ALERT,
210             LOCALE.get("ImageSelectorSearchOption"),
211             LOCALE.get("ImageSelectorCancelOption")
212         );
213         if (result == 0) {  // Yes, please search
214             FileChooser chooser = Env.getFileChooser();
215             String title = LOCALE.get(
216                 "ImageSelectorFindDialogTitle", oldImageFile.getName()
217             );
218             file = chooser.openFile(
219                 title, chooserFile, parent, ImageFilenameFilter.INSTANCE
220             );
221         }
222         else {
223             file = null;    // Cancel
224         }
225         return file;
226     }
227 
main(String[] args)228     public static void main(String[] args) {
229         File docFile = new File("/DocumentFile.lzn");
230         File oldImageFile = new File("/OldImage.tif");
231         File[] files = new File[] {
232             new File("a"), new File("b"), new File("c")
233         };
234         File newImageFile = DocumentImageSelector.chooseImageFile(
235             docFile, oldImageFile, files, null, null
236         );
237         System.out.println(newImageFile);
238 
239         System.exit(0);
240     }
241 }
242