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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
| package oracle.forms.fd;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.io.File;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.filechooser.*;
/**
* A javabean to display an Open-dialog bow with image preview
*
* @version 1.0
* @date 20-oct-2005
* @author Francois Degrelle
*/
public class GetImageFileName extends Component
{
public String string = "" ;
public GetImageFileName()
{
super();
}
/**
* method to display the OpenFile dialog.
*
* @param the title for the dialog
* @param the starting directory
* @return the selected file name (or null)
*/
public String GetFile(String dialogTitle, String startDirectory)
{
String sFileName = "" ;
Filters.setTitle( dialogTitle ) ;
Filters.setDir( startDirectory ) ;
sFileName = Filters.Start() ;
return sFileName ;
}
public void setString(String s)
{
string = s ;
System.out.println("s="+string);
}
public String getString()
{
return string ;
}
public void SetLAF(String s)
{
if(s.equalsIgnoreCase("SYSTEM"))
{
try
{
//System.out.println(UIManager.getLookAndFeelDefaults());
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else
{
try
{
//System.out.println(UIManager.getLookAndFeelDefaults());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
/**
* class that handle the JFileChooser
*/
class Filters extends JLabel implements PropertyChangeListener {
private static final int ICON_WIDTH = 150;
private static final int ICON_HEIGHT = 150;
private static String fileName = "" ;
private static String sTitle = "" ;
private static String startDir = "." ;
private static String sFilters = "" ;
// Set the dialog box title
public static void setTitle( String title )
{
sTitle = title ;
//System.out.println("**** sTitle="+sTitle);
}
// Set the start directory
public static void setDir( String directory )
{
startDir = directory ;
//System.out.println("**** startdir="+startDir);
}
// Add the image previewer to the dialog box
public Filters(JFileChooser chooser) {
setVerticalAlignment(JLabel.CENTER);
setHorizontalAlignment(JLabel.CENTER);
chooser.addPropertyChangeListener(this);
setPreferredSize(new Dimension (ICON_WIDTH, ICON_HEIGHT));
}
public void propertyChange(
PropertyChangeEvent changeEvent) {
String changeName = changeEvent.getPropertyName();
if (changeName.equals
(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
File file = (File)changeEvent.getNewValue();
if (file != null) {
ImageIcon icon = new ImageIcon(file.getPath());
if (icon.getIconWidth() > ICON_WIDTH) {
icon = new ImageIcon(
icon.getImage().getScaledInstance(ICON_WIDTH, -1, Image.SCALE_DEFAULT));
if (icon.getIconHeight() > ICON_HEIGHT) {
icon = new ImageIcon(
icon.getImage().getScaledInstance(-1, ICON_HEIGHT, Image.SCALE_DEFAULT));
}
}
setIcon(icon);
}
}
}
public static String Start() {
fileName = "" ;
JFileChooser fileChooser = new JFileChooser(startDir);
LabelAccessory accessory = new LabelAccessory(fileChooser);
fileChooser.setDialogTitle(sTitle);
fileChooser.setLocation(50,50);
fileChooser.setPreferredSize(new Dimension(600,400));
// Set the filter
ExampleFileFilter filter = new ExampleFileFilter("jpg");
filter.addExtension("jpeg");
filter.addExtension("gif");
filter.addExtension("png");
filter.setDescription("Jpg,Jpeg,Gif & Png Files");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setAccessory(accessory);
fileChooser.addPropertyChangeListener
(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, accessory);
int status = fileChooser.showOpenDialog(null);
fileName = "" ;
if (status == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
fileName = selectedFile.getParent() + selectedFile.separator + selectedFile.getName() ;
}
return fileName ;
}
public static class LabelAccessory extends JLabel implements PropertyChangeListener {
private static final int ICON_WIDTH = 150;
private static final int ICON_HEIGHT = 150;
public LabelAccessory(JFileChooser chooser) {
setVerticalAlignment(JLabel.CENTER);
setHorizontalAlignment(JLabel.CENTER);
chooser.addPropertyChangeListener(this);
setPreferredSize(new Dimension(ICON_WIDTH, ICON_HEIGHT));
}
public void propertyChange(
PropertyChangeEvent changeEvent) {
String changeName = changeEvent.getPropertyName();
if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
File file = (File)changeEvent.getNewValue();
if (file != null) {
ImageIcon icon = new ImageIcon(file.getPath());
if (icon.getIconWidth() > ICON_WIDTH) {
icon = new ImageIcon(icon.getImage().getScaledInstance(ICON_WIDTH, -1, Image.SCALE_DEFAULT));
if (icon.getIconHeight() > ICON_HEIGHT) {
icon = new ImageIcon(icon.getImage().getScaledInstance(-1, ICON_HEIGHT, Image.SCALE_DEFAULT));
}
}
setIcon(icon);
}
}
}
}
/**
* A convenience implementation of FileFilter that filters out
* all files except for those type extensions that it knows about.
*
* Extensions are of the type ".foo", which is typically found on
* Windows and Unix boxes, but not on Macinthosh. Case is ignored.
*
* Example - create a new filter that filerts out all files
* but gif and jpg image files:
*
* JFileChooser chooser = new JFileChooser();
* ExampleFileFilter filter = new ExampleFileFilter(
* new String{"gif", "jpg"}, "JPEG & GIF Images")
* chooser.addChoosableFileFilter(filter);
* chooser.showOpenDialog(this);
*
* @version 1.9 04/23/99
* @author Jeff Dinkins
*/
public static class ExampleFileFilter extends FileFilter {
private String TYPE_UNKNOWN = "Type Unknown";
private String HIDDEN_FILE = "Hidden File";
private Hashtable filters = null;
private String description = null;
private String fullDescription = null;
private boolean useExtensionsInDescription = true;
/**
* Creates a file filter. If no filters are added, then all
* files are accepted.
*
* @see #addExtension
*/
public ExampleFileFilter() {
this.filters = new Hashtable();
}
/**
* Creates a file filter that accepts files with the given extension.
* Example: new ExampleFileFilter("jpg");
*
* @see #addExtension
*/
public ExampleFileFilter(String extension) {
this(extension,null);
}
/**
* Creates a file filter that accepts the given file type.
* Example: new ExampleFileFilter("jpg", "JPEG Image Images");
*
* Note that the "." before the extension is not needed. If
* provided, it will be ignored.
*
* @see #addExtension
*/
public ExampleFileFilter(String extension, String description) {
this();
if(extension!=null) addExtension(extension);
if(description!=null) setDescription(description);
}
/**
* Creates a file filter from the given string array.
* Example: new ExampleFileFilter(String {"gif", "jpg"});
*
* Note that the "." before the extension is not needed adn
* will be ignored.
*
* @see #addExtension
*/
public ExampleFileFilter(String[] filters) {
this(filters, null);
}
/**
* Creates a file filter from the given string array and description.
* Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
*
* Note that the "." before the extension is not needed and will be ignored.
*
* @see #addExtension
*/
public ExampleFileFilter(String[] filters, String description) {
this();
for (int i = 0; i < filters.length; i++) {
// add filters one by one
addExtension(filters[i]);
}
if(description!=null) setDescription(description);
}
/**
* Return true if this file should be shown in the directory pane,
* false if it shouldn't.
*
* Files that begin with "." are ignored.
*
* @see #getExtension
* @see FileFilter#accepts
*/
public boolean accept(File f) {
if(f != null) {
if(f.isDirectory()) {
return true;
}
String extension = getExtension(f);
if(extension != null && filters.get(getExtension(f)) != null) {
return true;
};
}
return false;
}
/**
* Return the extension portion of the file's name .
*
* @see #getExtension
* @see FileFilter#accept
*/
public String getExtension(File f) {
if(f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if(i>0 && i<filename.length()-1) {
return filename.substring(i+1).toLowerCase();
};
}
return null;
}
/**
* Adds a filetype "dot" extension to filter against.
*
* For example: the following code will create a filter that filters
* out all files except those that end in ".jpg" and ".tif":
*
* ExampleFileFilter filter = new ExampleFileFilter();
* filter.addExtension("jpg");
* filter.addExtension("tif");
*
* Note that the "." before the extension is not needed and will be ignored.
*/
public void addExtension(String extension) {
if(filters == null) {
filters = new Hashtable(5);
}
filters.put(extension.toLowerCase(), this);
fullDescription = null;
}
/**
* Returns the human readable description of this filter. For
* example: "JPEG and GIF Image Files (*.jpg, *.gif)"
*
* @see setDescription
* @see setExtensionListInDescription
* @see isExtensionListInDescription
* @see FileFilter#getDescription
*/
public String getDescription() {
if(fullDescription == null) {
if(description == null || isExtensionListInDescription()) {
fullDescription = description==null ? "(" : description + " (";
// build the description from the extension list
Enumeration extensions = filters.keys();
if(extensions != null) {
fullDescription += "." + (String) extensions.nextElement();
while (extensions.hasMoreElements()) {
fullDescription += ", " + (String) extensions.nextElement();
}
}
fullDescription += ")";
} else {
fullDescription = description;
}
}
return fullDescription;
}
/**
* Sets the human readable description of this filter. For
* example: filter.setDescription("Gif and JPG Images");
*
* @see setDescription
* @see setExtensionListInDescription
* @see isExtensionListInDescription
*/
public void setDescription(String description) {
this.description = description;
fullDescription = null;
}
/**
* Determines whether the extension list (.jpg, .gif, etc) should
* show up in the human readable description.
*
* Only relevent if a description was provided in the constructor
* or using setDescription();
*
* @see getDescription
* @see setDescription
* @see isExtensionListInDescription
*/
public void setExtensionListInDescription(boolean b) {
useExtensionsInDescription = b;
fullDescription = null;
}
/**
* Returns whether the extension list (.jpg, .gif, etc) should
* show up in the human readable description.
*
* Only relevent if a description was provided in the constructor
* or using setDescription();
*
* @see getDescription
* @see setDescription
* @see setExtensionListInDescription
*/
public boolean isExtensionListInDescription() {
return useExtensionsInDescription;
}
}
} |
Partager