Created
April 7, 2017 02:38
-
-
Save easai/c446de8e4ec8cb006b39dc0a60be07a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package easai.util; | |
import java.io.*; | |
import java.text.*; | |
import java.util.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.table.*; | |
public class EditProperties extends JDialog implements ActionListener | |
{ | |
Properties properties=null; | |
int nProperties=0; | |
private JButton cancel= new JButton("Cancel"); | |
private JButton save= new JButton("Save"); | |
private JScrollPane scroll= new JScrollPane(); | |
private JTable table; | |
public EditProperties(Properties properties) | |
{ | |
super((Frame)null, "Edit Properties", true); | |
this.properties=properties; | |
} | |
public Properties init() | |
{ | |
Container panel=getContentPane(); | |
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); | |
addWindowListener(new java.awt.event.WindowAdapter() | |
{ | |
public void windowClosing(java.awt.event.WindowEvent e) | |
{ | |
dispose(); | |
} | |
}); | |
// setting "properties" data to "values" array | |
for (Enumeration e = properties.propertyNames() ; e.hasMoreElements() ;) | |
{ | |
nProperties++; | |
e.nextElement(); | |
} | |
if(nProperties<=0) | |
dispose(); | |
Object values[][]=new Object[nProperties][2]; | |
int i=0; | |
for (Enumeration e = properties.propertyNames() ; e.hasMoreElements() ;) | |
{ | |
values[i][0]=e.nextElement(); | |
values[i][1]=properties.getProperty((String)(values[i][0])); | |
i++; | |
} | |
table=new JTable(values,new String []{"Property", "Values"}); | |
setAlwaysOnTop(true); | |
scroll.setViewportView(table); | |
panel.add(scroll); | |
save.addActionListener(this); | |
cancel.addActionListener(this); | |
JPanel control=new JPanel(); | |
control.setLayout(new FlowLayout()); | |
control.add(save); | |
control.add(cancel); | |
panel.add(control); | |
pack(); | |
setVisible(true); | |
return properties; | |
} | |
public String defaultProperty(String propertyName, String defaultValue) | |
{ | |
String property=""; | |
if((property=properties.getProperty(propertyName))==null | |
|| property.equals("")) | |
property=defaultValue; | |
properties.setProperty(propertyName,property); | |
return property; | |
} | |
public void actionPerformed(ActionEvent e) | |
{ | |
Object source=e.getSource(); | |
if(source==cancel) | |
{ | |
dispose(); | |
} | |
else | |
{ | |
String propertyName="", propertyValue; | |
TableCellEditor editor=null; | |
if(table!=null) | |
{ | |
editor=table.getCellEditor(); | |
if(editor!=null) | |
editor.stopCellEditing(); | |
Object object=null; | |
for(int i=0;i<nProperties;i++) | |
{ | |
object=table.getValueAt(i,0); | |
if(object!=null) | |
propertyName=(String)object; | |
object=table.getValueAt(i,1); | |
propertyValue=(String)object; | |
if(!propertyName.equals("") && propertyValue!=null) | |
{ | |
properties.setProperty(propertyName, propertyValue); | |
} | |
} | |
dispose(); | |
} | |
} | |
} | |
public void readProperties(String fileName) | |
{ | |
FileInputStream in=null; | |
try | |
{ | |
File file=new File(fileName); | |
if(file.exists() && file.length()>0) | |
{ | |
in=new FileInputStream(file); | |
properties.loadFromXML(in); | |
} | |
} | |
catch(Exception e){e.printStackTrace();} | |
finally | |
{ | |
try | |
{ | |
if(in!=null) | |
in.close(); | |
} | |
catch(Exception e){e.printStackTrace();} | |
} | |
} | |
public void writeProperties(String fileName) | |
{ | |
FileOutputStream out=null; | |
try | |
{ | |
out=new FileOutputStream(fileName); | |
DateFormat dateFormat=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); | |
properties.storeToXML(out,dateFormat.format(new java.util.Date())); | |
} | |
catch(Exception e) | |
{ | |
try | |
{ | |
JFileChooser dlg=new JFileChooser(properties.getProperty("photoDir")); | |
int retval=dlg.showDialog(this,"Select another properties file"); | |
File propertiesFile; | |
if(retval==JFileChooser.APPROVE_OPTION) | |
{ | |
propertiesFile=dlg.getSelectedFile(); | |
if(propertiesFile.exists()) | |
{ | |
out=new FileOutputStream(propertiesFile); | |
DateFormat dateFormat=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); | |
properties.storeToXML(out,dateFormat.format(new java.util.Date())); | |
} | |
} | |
}catch(Exception ex){ex.printStackTrace();} | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
try | |
{ | |
if(out!=null) | |
out.close(); | |
} | |
catch(Exception e){e.printStackTrace();} | |
} | |
} | |
// for debugging purposes only | |
public static void main(String args[]) | |
{ | |
Properties properties=new Properties(); | |
properties.setProperty("application name","EditProperties"); | |
properties.setProperty("author","easai"); | |
properties.setProperty("date created","Fri Dec 10 20:50:33 2004"); | |
properties.setProperty("debug","true"); | |
properties.setProperty("verbose","false"); | |
EditProperties editProperties=new EditProperties(properties); | |
properties=editProperties.init(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment