1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import javax.swing.text.JTextComponent ; 5 import java.awt.*; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import java.io.File ; 9 10 18 19 public class CBFileChooserButton extends CBButton implements ActionListener 20 { 21 static String globalLastDirectory = ""; String localLastDirectory = ""; boolean useLocal = false; 25 JTextComponent myText; 26 Component parent; 27 28 41 42 public CBFileChooserButton(JTextComponent text, Component GUIparent) 43 { 44 this(text, GUIparent, "File"); 45 } 46 47 62 63 public CBFileChooserButton(JTextComponent text, Component GUIparent, String buttonName) 64 { 65 this(text, GUIparent, buttonName, ""); 66 } 67 68 84 85 public CBFileChooserButton(JTextComponent text, Component GUIparent, String buttonName, String tooltip) 86 { 87 super(buttonName, tooltip, new ImageIcon("open.gif")); 88 myText = text; 89 parent = GUIparent; 90 addActionListener(this); 91 } 92 93 94 public void setLocalDirectoryUse(boolean state) 95 { 96 useLocal = state; 97 } 98 99 public boolean getLocalDirectoryUse() 100 { 101 return useLocal; 102 } 103 104 public static void setGlobalDirectory(String dirString) 105 { 106 globalLastDirectory = dirString; 107 } 108 109 public void setStartingDirectory(String dirString) 110 { 111 if (useLocal) 112 localLastDirectory = dirString; 113 else 114 setGlobalDirectory(dirString); 115 } 116 117 public String getStartingDirectory() 118 { 119 return (useLocal ? localLastDirectory : globalLastDirectory); 120 } 121 122 public void actionPerformed(ActionEvent e) 123 { 124 String lastDirectory = (useLocal) ? localLastDirectory : globalLastDirectory; 125 126 130 131 try 132 { 133 if (new File (lastDirectory).exists() == false) 134 { 135 lastDirectory = null; 136 } 137 } 138 catch (Exception ex) { 140 lastDirectory = null; 141 } 142 143 JFileChooser chooser = new JFileChooser(lastDirectory); 144 int option = chooser.showOpenDialog(parent); 145 146 if (option == JFileChooser.APPROVE_OPTION) { 148 setStartingDirectory(chooser.getSelectedFile().getParent()); 149 File readFile = chooser.getSelectedFile(); 150 myText.setText(readFile.toString()); 151 } 152 } 153 } | Popular Tags |