Let the user enter the filename - java

I created an interface that allows to add instances in an rdf file. I put the filepath in the readRDFfile parameter and the same filepath in Filewriter (in order to update the file when user add instances). But i'd like to allow user enter the name file he want to create when I execute the code. And FileWriter must take this file in parameter when user add instances.
My problem is that I don't know how to put the file that user has chosen and that was read in readRDFfile, in Filewriter parameter in order to be updated when he adds instances.
import java.util.*;
import java.util.List;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.ontology.impl.*;
import com.hp.hpl.jena.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.XSD;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class FamilyModel extends Frame
{
TextField[]tabTF=new TextField[4];
Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant, bValiderModif; //buttons Add, Remove, Previous, Next
OntModel model;
Onto onto;
int indice=0;
int p=0;
Resource p1;
Button creerBouton(String S, int x, int y)
{
Button b=new Button(S);
add(b);
b.setBounds(x,y,120,30);
return b;
}
void creerLabel(String etiquette, int x, int y)
{
Label la=new Label(etiquette);
la.setBounds(x,y,100,25);
add(la);
}
public FamilyModel ()
{
setLayout (null);
setBackground (Color.pink);
setBounds (100,200,900,450);
creerLabel("Prenom : ",10,50);
creerLabel("Nom : ",10,100);
creerLabel("Date de Naissance: ",10,145);
creerLabel("Genre (H ou F): ",10,190);
//TextFields
for(int i=0;i<4;i++)
{
tabTF[i]=new TextField("");
tabTF[i].setBackground(Color.white);
add(tabTF[i]);
}
tabTF[0].setBounds(120,45,150,25);
tabTF[1].setBounds(120,100,150,25);
tabTF[2].setBounds(120,145, 100,25);
tabTF[3].setBounds(120,190, 45,25);
bAjout=creerBouton("Ajouter",20,250);
setVisible(true);
bModifier=creerBouton("Modifier",138,250);
setVisible(true);
//bSupprimer=creerBouton("Supprimer",250,250);
//setVisible(true);
bPrecedent=creerBouton("Precedent",360,250);
bSuivant=creerBouton("Suivant",450,250);
bSupprimer=creerBouton("Supprimer",600,250);
setVisible(true);
onto = new Onto();
readRDFfile();
traitement(this);
}
void traitement(Frame fenetre)
{
bAjout.addActionListener(new ActionAjoutPersonne());
//bModifier.addActionListener(new ActionModifier());
//bValiderModif.addActionListener(new ActionModif());
bSuivant.addActionListener(new ActionSuivant());
bPrecedent.addActionListener(new ActionPrecedent());
bSupprimer.addActionListener(new ActionSupprimer());
}
//Button Add
public class ActionAjoutPersonne implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
p1=onto.model.createResource(onto.uriBase+"#"+tabTF[0].getText());
p1.addProperty(onto.aPourPrenom, tabTF[0].getText());
p1.addProperty(onto.aPourNom, tabTF[1].getText());
p1.addProperty(onto.aDateNaiss, tabTF[2].getText());
if (tabTF[3].getText().equals("F"))
{
p1.addProperty(onto.aGenre, tabTF[3].getText());
p1.addProperty(RDF.type, onto.femme);
}
else if (tabTF[3].getText().equals("H"))
{
p1.addProperty(onto.aGenre, tabTF[3].getText());
p1.addProperty(RDF.type, onto.homme);
}
StringWriter sw = new StringWriter();
onto.model.write(sw, "RDF/XML-ABBREV");
String owlCode = sw.toString();
File file = new File("d:/Onto.rdf");
try{
FileWriter fw = new FileWriter(file);
fw.write(owlCode);
fw.close();
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
//Button Remove
public class ActionSupprimer implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
List<Statement> statementsToRemove = new ArrayList<Statement>();
StmtIterator iter = onto.model.listStatements();
while (iter.hasNext())
{
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property predicate = stmt.getPredicate();
RDFNode object = stmt.getObject();
if(subject.toString().equals (onto.uriBase+"#"+tabTF[0].getText()))
{
statementsToRemove.add(stmt);
}
}
for( Statement stmt : statementsToRemove)
{
onto.model.remove(stmt);
}
StringWriter sw = new StringWriter();
onto.model.write(sw, "RDF/XML-ABBREV");
String owlCode = sw.toString();
File file = new File("d:/Onto.rdf");
try{
FileWriter fw = new FileWriter(file);
fw.write(owlCode);
fw.close();
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
//Read Onto.rdf
public void readRDFfile()
{
String inputFile="D:/Onto.rdf";
try
{
InputStream in =new FileInputStream(inputFile);
if (in == null) {
System.out.println("File not found");
}
onto.model.read(in, null);
}catch(Exception e) {
System.out.println("model.read catched error: " + e);
}
}
//Button Next
class ActionSuivant implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
++indice;
ExtendedIterator instances = onto.personne.listInstances();
Individual instance = null;
Individual firstInstance = null;
for (p = 0; p < indice && instances.hasNext(); p++) {
instance = (Individual) instances.next();
if (firstInstance == null) {
firstInstance = instance;
}
}
if (p < indice) {
indice = 1;
instance = firstInstance;
}
tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());
}
}
class ActionPrecedent implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
--indice;
//Instances de la Classe Personne
ExtendedIterator instances=onto.personne.listInstances();
Individual instance = null;
for(p = 0; p < indice && instances.hasNext(); p++)
{
instance = (Individual) instances.next();
}
tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());
}
}
//Ontology
public class Onto
{
OntClass personne, genre, homme, femme, feminin, masculin, evenement, deces, mariage, divorce;
OntModel model;
String uriBase;
ObjectProperty aPourFils, aPourFille, aGenre;
DatatypeProperty aPourNom, aPourPrenom, aDateNaiss;
public Onto (){
model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );
uriBase = "http://www.something.com/FAM";
model.createOntology(uriBase);
//Classes
personne = model.createClass(uriBase+"personne");
femme = model.createClass(uriBase+"femme");
homme = model.createClass(uriBase+"homme");
genre = model.createClass(uriBase+"genre");
feminin = model.createClass(uriBase+"feminin");
masculin = model.createClass(uriBase+"masculin");
evenement = model.createClass(uriBase+"evenement");
deces = model.createClass(uriBase+"deces");
mariage = model.createClass(uriBase+"mariage");
divorce = model.createClass(uriBase+"divorce");
//Sub-classes
genre.addSubClass(feminin);
genre.addSubClass(masculin);
personne.addSubClass(homme);
personne.addSubClass(femme);
evenement.addSubClass(deces);
evenement.addSubClass(mariage);
evenement.addSubClass(divorce);
aPourFils = model.createObjectProperty(uriBase+"aPourFils");
aPourFils.setDomain(personne);
aPourFils.setRange(homme);
aPourFille = model.createObjectProperty(uriBase+"aPourFille");
aPourFille.setDomain(personne);
aPourFille.setRange(femme);
aGenre = model.createObjectProperty(uriBase+"aGenre");
aGenre.setDomain(personne);
aGenre.setRange(genre);
aPourNom = model.createDatatypeProperty(uriBase+"aPourNom");
aPourNom.setDomain(personne);
aPourNom.setRange(XSD.xstring);
aPourPrenom = model.createDatatypeProperty(uriBase+"aPourPrenom");
aPourPrenom.setDomain(personne);
aPourPrenom.setRange(XSD.xstring);
aDateNaiss = model.createDatatypeProperty(uriBase+"aDateNaiss");
aDateNaiss.setDomain(personne);
aDateNaiss.setRange(XSD.xstring);
}
}
public static void main(String args[])
{
new FamilyModel();
}
}

If your app has a GUI, the standard way to select an input file would be to use a file chooser, for example JFileChooser if your app is Swing based, or FileDialog if you want to stick to AWT components.
Here is an exmaple for JFileChooser:
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String filename = file.getName();
}
JFileChooser Tutorial:
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
If your app is command line based (which I gather it not the case from your code for handling button clicks), you could make the input be one of the command line arguments when you run the app, and you could read it out of args[] array passed into main().

There are many options and schools of thought regarding how to get data from the user. Most of this depends on who the user will be and how they are going to be interacting with this program. A couple I've listed.
Command Line Interface This is my favorite because I'm in the terminal a lot and write a lot of bash scripts. Very simple and low development overhead, while still being extensible. You'll need to add code in your main method to retrieve options and values. I like using Apache Commons CLI even though it's not under active development, there is a good tutorial.
Graphical User Interface Use Java Swing or a web application to create a UI around your app. This will inevitably take much longer than any other option but will be the most accessible for non-technical users.
Standard In If you just want the program to pause and prompt the user on the console with no bells or whistles use this in main (if you using
Console con = System.console();
String file = con.readLine("File Name: ");

Related

sorting files using multithreading in java

I was given an assignment to write all ordered contents of given files into a result.txt. At first, the filenames are split into different Arraylists where each file contains a label in a format #n/N where N is the total number of files. e.g.
British explorer James Clark Ross led the first
expedition to reach the north magnetic pole
#001/004
from a file 1831-06-01.txt
The problem with my code is that it has written in order 1,4,2,3 respectively. However, the result must be in order 1,2,3,4. This may be due to a lack of synchronization. Nonetheless, I am still struggling to fix the problem.
This is my code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
class PopThread implements Runnable {
ArrayList<String> fileList;
public PopThread(ArrayList<String> fileList) {
this.fileList = fileList;
}
#Override
public void run() {
//System.out.println("running\n");
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
long startTime = System.nanoTime();
System.out.println("fileList: " + fileList);
ArrayList<String> sortedFileList = sortFiles(fileList);
File resultFile = new File("result.txt");
for (String filename : sortedFileList) {
Writer w1 = new Writer(filename, resultFile);
Thread t = new Thread(w1);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
long stopTime = System.nanoTime();
//System.out.println("Total execution time: " + (stopTime - startTime));
}
public ArrayList<String> readFiles(String filename) {
ArrayList<String> list = new ArrayList<String>();
try {
File myObj = new File(filename);
Scanner s = new Scanner(myObj);
while (s.hasNext()) {
list.add(s.next());
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
public int getNumber(String filename) {
String lastLine = "";
String sCurrentLine;
int identifier_integer = -1;
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
while ((sCurrentLine = br.readLine()) != null) {
lastLine = sCurrentLine;
}
String identifier_number = lastLine.substring(1,4);
identifier_integer = Integer.parseInt(identifier_number);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return identifier_integer;
}
public ArrayList<String> sortFiles(ArrayList<String> listFileName) {
int i = listFileName.size();
boolean sorted = false;
while ( (i > 1) && (!(sorted)) ) {
sorted = true;
for (int j = 1; j < i; j++) {
if ( getNumber(listFileName.get(j-1)) > getNumber(listFileName.get(j)) ) {
String temp = listFileName.get(j-1);
listFileName.set(j-1, listFileName.get(j));
listFileName.set(j, temp);
sorted = false;
}
}
i--;
}
return listFileName;
}
}
class Writer implements Runnable {
String filename;
File resultFile;
public Writer(String filename, File resultFile) {
this.filename = filename;
this.resultFile = resultFile;
}
#Override
public void run() {
String content;
content = readFromFile(filename);
writeToFile(resultFile, content);
}
private static void writeToFile(File resultFile, String content) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(resultFile, true));
writer.write(content);
//writer.write("file content written");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
static String readFromFile(String filename) {
StringBuffer content = new StringBuffer();
try {
String text;
BufferedReader reader = new BufferedReader(new FileReader(filename));
while ((text = reader.readLine()) != null) {
content.append(text);
content.append("\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
}
public class q4 {
public static void main(String[] args) {
ArrayList<String> filesOne = new ArrayList<String>();
filesOne.add("1831-06-01.txt");
filesOne.add("2003-08-27.txt");
ArrayList<String> filesTwo = new ArrayList<String>();
filesTwo.add("1961-04-12.txt");
filesTwo.add("1972-12-11.txt");
PopThread popRunnableOne = new PopThread(filesOne);
PopThread popRunnableTwo = new PopThread(filesTwo);
Thread threadOne = new Thread(popRunnableOne);
Thread threadTwo = new Thread(popRunnableTwo);
threadOne.start();
threadTwo.start();
try {
threadOne.join();
threadTwo.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
( NOTE: The class q4 cannot be altered)
This assignment is horrible. You have my sympathy.
Your two threads will have to communicate with each other. Each thread will have to know, what is the filename that the other thread wants to output next. And, they will have to take turns. Each thread needs to loop:
While the date on my next file is less than or equal to the date on the other thread's next file, output my next file,
Tell the other thread, "it's your turn,"
If I have no more files, then exit (return from the run() method), otherwise, wait for the other thread to tell me it's my turn again,
Go back to step 1.
Having to take turns is the worst part of the assignment. Any time you find yourself needing to make threads take turns doing something—any time you need to make threads do things in a particular order—that's a clear sign that all of the things should be done by a single thread.
The only way threads can communicate is through shared variables. Your instructor has done you a huge disservice by telling you not to modify the q4 class. That prevents you from passing any shared objects in to your PopThread implementation through its constructor.
The only other way your two threads can share any variables is by making the variables static. Forcing you to use static is the second worst part of the assignment. If you go on to study software engineering, you will learn that static is an anti-pattern. Programs that use static variables are brittle (i.e., hard to modify), and they are hard to test.
Forcing you to use static variables also will make your threads do extra work to figure out who is who. Normally, I would do something like this so that each thread would automatically know which state is its own, and which belongs to the other guy:
class SharedState { ... }
class PopThread {
public PopThread(
SharedState myState,
SharedState otherThreadState,
ArrayList<String> fileList
) {
this.myState = myState;
this.otherGuyState = otherThreadState;
this.fileList = fileList;
...initialize this.myState...
}
...
}
class q4 {
public static void main(String[] args) {
SharedState stateOne = new SharedState();
SharedState stateTwo = new SharedState();
PopThread popRunnableOne = new PopThread(stateOne, stateTwo, filesOne);
PopThread popRunnableTwo = new PopThread(stateTwo, stateOne, filesTwo);
...
}
}
The best way I can think of with static variables would be to have an array of two SharedState, and have the threads use an AtomicInteger to each assign themself one of the two array slots:
class PopThread {
static SharedState[] state = new SharedState [2];
static AtomicInteger nextStateIndex = new AtomicInteger(0);
public PopThread(
SharedState myState,
SharedState otherThreadState,
ArrayList<String> fileList
) {
myStateIndex = nextStateIndex.getAndIncrement();
otherGuysStateIndex = myStateIndex ^ 1;
this.fileList = fileList;
...initialize state[myStateIndex]...
}
...
}

Storing Objects of an array into CSV file and reading them with specific arguments for GUI

As a part of my assignment I had to store objects of an array in a flat-file and retrieve them when certain criteria was met. I can save the objects fine but when retrieving them I have an issue with getting more than one value, I understand what is going wrong but I am struggling to find a solution. Here is the concept of whats happening.
Button no 10,A (R1S10 in the code)is my testing button, When I click it it creates an event that I will show below.
Click event for button 10A -
private void R1S10ActionPerformed(java.awt.event.ActionEvent evt) {
seats.add(seat1);
if (R1S10.getBackground().equals(Color.red) &&(IsSeatBooked().equals("true"))){
Component frame = null;
JOptionPane.showMessageDialog(frame, "Seat UnBooked");
seat1.setBooked("false");
seat1.setName("");
R1S10.setBackground(Color.yellow);
try {
reader();
writer();
//String booked = "true";
//Pass String booked into csv file
} catch (IOException ex) {
Logger.getLogger(SeatingPlan.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
Component frame = null;
String name = JOptionPane.showInputDialog(frame, "Please enter name of Customer booking");
if (name.isEmpty()) {
JOptionPane.showMessageDialog(frame, "No value entered");
} else if (name != null) {
seat1.setName(name);
seat1.setBooked("true");
R1S10.setBackground(Color.red);
JOptionPane.showMessageDialog(frame, "Your Booking has been placed");
try {
writer();
reader();
//String booked = "true";
//Pass String booked into csv file
} catch (IOException ex) {
Logger.getLogger(SeatingPlan.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Followed by the screen below -
Outcome -
And when the button is pressed again -
I am using three methods in this SeatingPlan.java - writer(),reader() and IsSeatBooked().
SeatingPlan -
public class SeatingPlan extends javax.swing.JFrame {
/**
* Creates new form SeatingPlan
*/
String seatNo, name, bookedSeat;
FileWriter fileWriter = null;
List<Seat> seats = new ArrayList<Seat>();
//Seat Object Declaration
Seat seat1 = new Seat("R1S10","","false");
Seat seat2 = new Seat("R1S9", "", "false");
String fileName = "seat.csv";
writer -
public void writer() throws IOException {
//Delimiter used in CSV file
final String NEW_LINE_SEPARATOR = "\n", COMMA_DELIMITER = ",";
//CSV file header
final String FILE_HEADER = "seatID,name,booked";
//fileName = System.getProperty("user.home") + "/seat.csv";
try {
fileWriter = new FileWriter(fileName);
//Write the CSV file header
fileWriter.append(FILE_HEADER.toString());
//Add a new line separator after the header
fileWriter.append(NEW_LINE_SEPARATOR);
//Write a new student object list to the CSV file
for (Seat seat : seats) {
fileWriter.append(String.valueOf(seat.getSeatID()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(seat.getName());
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(seat.isBooked());
fileWriter.append(NEW_LINE_SEPARATOR);
}
System.out.println("CSV file was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
fileWriter.flush();
fileWriter.close();
}
}
reader -
public void reader() {
//Delimiter used in CSV file
final String COMMA_DELIMITER = ",";
//Student attributes index
final int SEAT_ID_IDX = 0;
final int SEAT_NAME_IDX = 1;
final int SEAT_BOOKED = 2;
//private static final int STUDENT_LNAME_IDX = 2;
BufferedReader fileReader = null;
try {
//Create a new list of student to be filled by CSV file data
List<Seat> seats = new ArrayList<>();
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileName));
//Read the CSV file header to skip it
fileReader.readLine();
//Read the file line by line starting from the second line
while ((line = fileReader.readLine()) != null) {
//Get all tokens available in line
String[] tokens = line.split(COMMA_DELIMITER);
if (tokens.length > 0) {
//Create a new seat object and fill his data
Seat seat = new Seat(tokens[SEAT_ID_IDX],
tokens[SEAT_NAME_IDX], tokens[SEAT_BOOKED]);
seats.add(seat);
seatNo = tokens[SEAT_ID_IDX];
//System.out.println("Seat Number: " + seatNo);
bookedSeat = tokens[SEAT_BOOKED];
}
}
//Print the new student list
for (Seat seat : seats) {
System.out.println(seat.toString());
}
} catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}
}//end reader
SeatingPlan - This if where I have tried to have the arguments controlling the outcome but IsBooked is colliding when multiple seats are selected.
public SeatingPlan() throws IOException {
setVisible(true);
initComponents();
//reader();
ColourSectionGold();
ColourSectionBronze();
reader();
if(R1S10.getBackground().equals(Color.yellow) && (IsSeatBooked().equals("true"))){ R1S10.setBackground(Color.red);}
//if(R1S9.getBackground().equals(Color.yellow) && (IsSeatBooked().equals("true2"))){ R1S9.setBackground(Color.red);}
}
IsSeatBooked -
public String IsSeatBooked(){
return bookedSeat;
}//end IsSeatBooked
Im using the method above as my argument to see whether a seat is booked or not, but when a new seat is click it sets the whole value of 'bookedSeat' - which leaves the system not working correctly. I understand the code is not very efficient but is there any temporary fix for this problem, if I have explained it correctly.
Also I will include my class for Seat -
public class Seat {
private String seatID;
private String booked;
private String name;
private int price;
public Seat(String seatID,String name,String booked){
this.seatID = seatID;
this.booked = "";
this.name = name;
this.price = price;
}
public String getSeatID() {
return seatID;
}
public void setSeatID(String seatID) {
this.seatID = seatID;
}
public String isBooked() {
return booked;
}
public void setBooked(String booked) {
this.booked = booked;
}
public String getStatus(){
return booked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPrice() {
this.price = price;
}}//end class Seat
And a look at the CSV file that is created -
I wish to be able to click more than one button and save its state, Button 10 works fine at the moment, but as IsBooked only has one value at a time it clashes.
If you took the time to check this out, I appreciate it. Any constructive criticism is helpful and any ideas would be great!
Thanks,
Paddy.
Too much code to look at to see exactly what you are doing.
Instead of using your csv file, you could create a Properties file. The Propertiesfile will store the data in the form of:
key:data
So in your case the key would be the id: A1, A2... and the data would be the name of the person who booked the seat.
So the file would start out as empty. When you create the GUI you would create a loop that checks each id to see if an entry is found in the Properties field. If it is found then you display the seat as taken, otherwise it is empty.
Then whenever you want to book a seat you just use the setProperty(...) method.
The Properties class has load(...) and store(...) methods.
So the Properties class allows you to easily manage a flat file database with minimal effort.
Note, you would never have variable names like R1S10. That would requirement 100 different variables with if/else statements. Instead you would extend JButton and pass in the row and seat as parameters the button. Then in the ActionListener for the button you can access the row/seat information to built the ID used as the key for the properties file.
Edit:
Couldn't quite make the loop that checks if the ID is in the properties file.
If the property is null, the seath is empty.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Properties properties = new Properties();
properties.setProperty("A2", "Smith");
properties.setProperty("C3", "Jones");
String[] rows = { "A", "B", "C", "D" };
int seats = 4;
for (int row = 0; row < rows.length; row++)
{
for (int seat = 1; seat <= seats; seat++)
{
String key = rows[row] + seat;
String property = properties.getProperty( key );
System.out.println(key + " : " + property);
}
}
}
}

Using persistent layout with custom nodes and edges

I'm using the Jung library to create graphs. I want to use the persistent layout to generate the same graph every time.
I copied pasted this working code:
http://jung.sourceforge.net/site/jung-samples/xref/edu/uci/ics/jung/samples/PersistentLayoutDemo.html?
And all I changed was the graph that was being created. The graph I created has custom nodes and edges. But now when I try to to restore I get (im using eclipse):
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at helloworld.main.main(main.java:161)
And when i go to the problems view in eclipse(i was at console before) there are 2 errors:
1) Syntax error on token ";", { expected after this token
This is at the line:
BufferedReader reader = new BufferedReader(new FileReader("Topology2.csv"));
2)Syntax error, insert "}" to complete Block
This is at the line :
PersistentLayout<nodes,Edges> persistentLayout;
This is my code:
public class main {
/**
* the graph
*/public static nodes find(String a, ArrayList<nodes> b) {
for (int i = 0; i < b.size(); i++) {
if (a.compareTo(b.get(i).name) == 0){
return b.get(i);
}
}
//System.out.println("Couldnt find node: " + a);
return b.get(0);
}
/**
* the name of the file where the layout is saved
*/
String fileName;
/**
* the visual component and renderer for the graph
*/
VisualizationViewer<nodes,Edges> vv;
PersistentLayout<nodes,Edges> persistentLayout;
/**
* create an instance of a simple graph with controls to
* demo the persistence and zoom features.
*
* #param fileName where to save/restore the graph positions
*/
public main(final String fileName) throws Exception {
Graph<nodes, Edges> g = new SparseMultigraph<nodes, Edges>();
ArrayList<nodes> araylist = new ArrayList<nodes>();
String line = null;
BufferedReader reader = new BufferedReader(new FileReader("Topology2.csv"));
while ((line = reader.readLine()) != null) {
nodes newNode = new nodes (line);
araylist.add(newNode);
}
reader.close();
BufferedReader reader2 = new BufferedReader(new FileReader("Topology.csv"));
int counter = 0;
while ((line = reader2.readLine()) != null) {
counter++;
String[] a = line.split(",");
int number = a.length;
//System.out.println("length: " + number);
nodes origin = find(a[0],araylist);
nodes prev = find(a[1], araylist);
if (number >= 3) {
nodes next = find(a[2], araylist);
g.addEdge(new Edges(counter,origin,next),origin, next);
}
g.addEdge(new Edges(counter,origin,prev),origin, prev);
counter++;
}
reader2.close();
this.fileName = fileName;
// create a simple graph for the demo
persistentLayout =
new PersistentLayoutImpl<nodes,Edges>(new FRLayout<nodes,Edges>(g));
vv = new VisualizationViewer<nodes,Edges>(persistentLayout);
// add my listener for ToolTips
vv.setVertexToolTipTran sformer(new ToStringLabeller());
DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
vv.setGraphMouse(gm);
// create a frome to hold the graph
final JFrame frame = new JFrame();
frame.getContentPane().add(new GraphZoomScrollPane(vv));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a control panel and buttons for demo
// functions
JPanel p = new JPanel();
JButton persist = new JButton("Save Layout");
// saves the graph vertex positions to a file
persist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
persistentLayout.persist(fileName);
} catch (IOException e1) {
System.err.println("got "+e1);
}
}
});
p.add(persist);
JButton restore = new JButton("Restore Layout");
// restores the graph vertex positions from a file
// if new vertices were added since the last 'persist',
// they will be placed at random locations
restore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// PersistentLayout<nodes,Edges> pl = (PersistentLayout<nodes,Edges>) vv.getGraphLayout();
try {
persistentLayout.restore(fileName);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
p.add(restore);
p.add(gm.getModeComboBox());
frame.getContentPane().add(p, BorderLayout.SOUTH);
frame.pack();//setSize(600, 600);
frame.setVisible(true);
}
/**
* a driver for this demo
* #param args should hold the filename for the persistence demo
* #throws Exception
*/
public static void main(String[] args) throws Exception {
String filename;
if (args.length >= 1)
filename = args[0];
else
filename = "PersistentLayoutDemo.out";
new main(filename);
}
}
Code for custom edges:
public class Edges {
int id;
nodes start;
nodes end;
public Edges(int id_, nodes a, nodes b) {
id = id_;
start = a;
end = b;
}
}
Code for custom nodes:
package helloworld;
import java.io.Serializable;
public class nodes {
String name = "";
nodes next = null;
nodes prev = null;
boolean alarm = false;
String alarmLevel = "";
String alarmCaption = "";
String alarmInfo = "";
public nodes(String na) {
name = na;
}
public nodes(String na, String n, String p, boolean a, String lvl, String cap, String info) {
name = na;
next = new nodes();
prev = new nodes();
next.name = n;
prev.name = p;
alarm = a;
alarmLevel = lvl;
alarmCaption = cap;
alarmInfo = info;
}
public nodes() {
// TODO Auto-generated constructor stub
}
}
Inside the nodes constructor you're assigning next.name but never initializing next, which is null. That's at least part of your problem.
Side note: your edge class has references to two nodes objects for no readily apparent reason, and I'm not sure what the prev and next are for in the nodes class, either.
You need to figure out what your data model is supposed to be, and bear in mind that the Graph class is keeping track of the node/edge connections.

I cannot remove values of data type properties in Jena

I'm trying to remove values of datatype properties of an instance through an interface which I created in java but it does not work. It gives me an
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException.
I don't understand why. Can you please explain me what's wrong?
Here is my code for button remove:
//Button Remove
public class ActionRemove implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
StmtIterator iter = onto.model.listStatements();
while (iter.hasNext())
{
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property predicate = stmt.getPredicate();
RDFNode object = stmt.getObject();
if(subject.toString().equals (onto.uriBase+"#"+tabTF[0].getText()))
{
onto.model.remove(stmt);
}
}
}
}
My complete code:
import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.ontology.impl.*;
import com.hp.hpl.jena.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.XSD;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class FamilyModel extends Frame
{
TextField[]tabTF=new TextField[4];
Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant, bRemove; //buttons Add, Remove, Previous, Next
OntModel model;
Onto onto;
int indice=0;
int p=0;
Resource p1;
Button creerBouton(String S, int x, int y)
{
Button b=new Button(S);
add(b);
b.setBounds(x,y,120,30);
return b;
}
void creerLabel(String etiquette, int x, int y)
{
Label la=new Label(etiquette);
la.setBounds(x,y,100,25);
add(la);
}
public FamilyModel ()
{
setLayout (null);
setBackground (Color.pink);
setBounds (100,200,900,450);
addWindowListener(new FermerFenetre());
creerLabel("Prenom : ",10,50);
creerLabel("Nom : ",10,100);
creerLabel("Date de Naissance: ",10,145);
creerLabel("Genre (H ou F): ",10,190);
//TextFields
for(int i=0;i<4;i++)
{
tabTF[i]=new TextField("");
tabTF[i].setBackground(Color.white);
add(tabTF[i]);
}
tabTF[0].setBounds(120,45,150,25);
tabTF[1].setBounds(120,100,150,25);
tabTF[2].setBounds(120,145, 100,25);
tabTF[3].setBounds(120,190, 45,25);
bAjout=creerBouton("Ajouter",20,250);
setVisible(true);
bModifier=creerBouton("Modifier",138,250);
setVisible(true);
bSupprimer=creerBouton("Supprimer",250,250);
setVisible(true);
bPrecedent=creerBouton("Precedent",360,250);
bSuivant=creerBouton("Suivant",450,250);
bRemove=creerBouton("Supprimer",600,250);
setVisible(true);
onto = new Onto();
readRDFfile();
traitement(this);
}
void traitement(Frame fenetre)
{
bAjout.addActionListener(new ActionAjoutPersonne());
bModifier.addActionListener(new ActionRemove());
bSuivant.addActionListener(new ActionSuivant());
bPrecedent.addActionListener(new ActionPrecedent());
bRemove.addActionListener(new ActionRemove());
}
//Button Add
public class ActionAjoutPersonne implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
p1=onto.model.createResource(onto.uriBase+"#"+tabTF[0].getText());
p1.addProperty(onto.aPourPrenom, tabTF[0].getText());
p1.addProperty(onto.aPourNom, tabTF[1].getText());
p1.addProperty(onto.aDateNaiss, tabTF[2].getText());
if (tabTF[3].getText().equals("F"))
{
p1.addProperty(onto.aGenre, tabTF[3].getText());
p1.addProperty(RDF.type, onto.femme);
}
else if (tabTF[3].getText().equals("H"))
{
p1.addProperty(onto.aGenre, tabTF[3].getText());
p1.addProperty(RDF.type, onto.homme);
}
StringWriter sw = new StringWriter();
onto.model.write(sw, "RDF/XML-ABBREV");
String owlCode = sw.toString();
File file = new File("d:/Onto.rdf");
try{
FileWriter fw = new FileWriter(file);
fw.write(owlCode);
fw.close();
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
//Button Remove
public class ActionRemove implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
StmtIterator iter = onto.model.listStatements();
while (iter.hasNext())
{
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property predicate = stmt.getPredicate();
RDFNode object = stmt.getObject();
if(subject.toString().equals (onto.uriBase+"#"+tabTF[0].getText()))
{
onto.model.remove(stmt);
}
}
}
}
//Read Onto.rdf
public void readRDFfile()
{
String inputFile="D:/Onto.rdf";
try
{
InputStream in =new FileInputStream(inputFile);
if (in == null) {
System.out.println("File not found");
}
onto.model.read(in," ");
}catch(Exception e) {
System.out.println("model.read catched error: " + e);
}
}
//Button Next
class ActionSuivant implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
++indice;
ExtendedIterator instances = onto.personne.listInstances();
Individual instance = null;
Individual firstInstance = null;
for (p = 0; p < indice && instances.hasNext(); p++) {
instance = (Individual) instances.next();
if (firstInstance == null) {
firstInstance = instance;
}
}
if (p < indice) {
indice = 1;
instance = firstInstance;
}
tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());
}
}
//Button Previous
class ActionPrecedent implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
--indice;
//Instances de la Classe Personne
ExtendedIterator instances=onto.personne.listInstances();
Individual instance = null;
for(p = 0; p < indice && instances.hasNext(); p++)
{
instance = (Individual) instances.next();
}
tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());
}
}
//Close window when X is pressed
public class FermerFenetre extends WindowAdapter
{
public void windowClosing(WindowEvent evt)
{
if(evt.getWindow().getName().equals("frame0"))
{
System.exit(0);
}
else
{
evt.getWindow().dispose();
}
}
}
//Ontology
public class Onto
{
OntClass personne, genre, homme, femme, feminin, masculin, evenement, deces, mariage, divorce;
OntModel model;
String uriBase;
ObjectProperty aPourFils, aPourFille, aGenre;
DatatypeProperty aPourNom, aPourPrenom, aDateNaiss;
public Onto (){
model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );
uriBase = "http://www.something.com/FAM";
model.createOntology(uriBase);
//Classes
personne = model.createClass(uriBase+"personne");
femme = model.createClass(uriBase+"femme");
homme = model.createClass(uriBase+"homme");
genre = model.createClass(uriBase+"genre");
feminin = model.createClass(uriBase+"feminin");
masculin = model.createClass(uriBase+"masculin");
evenement = model.createClass(uriBase+"evenement");
deces = model.createClass(uriBase+"deces");
mariage = model.createClass(uriBase+"mariage");
divorce = model.createClass(uriBase+"divorce");
//Sub-classes
genre.addSubClass(feminin);
genre.addSubClass(masculin);
personne.addSubClass(homme);
personne.addSubClass(femme);
evenement.addSubClass(deces);
evenement.addSubClass(mariage);
evenement.addSubClass(divorce);
aPourFils = model.createObjectProperty(uriBase+"aPourFils");
aPourFils.setDomain(personne);
aPourFils.setRange(homme);
aPourFille = model.createObjectProperty(uriBase+"aPourFille");
aPourFille.setDomain(personne);
aPourFille.setRange(femme);
aGenre = model.createObjectProperty(uriBase+"aGenre");
aGenre.setDomain(personne);
aGenre.setRange(genre);
aPourNom = model.createDatatypeProperty(uriBase+"aPourNom");
aPourNom.setDomain(personne);
aPourNom.setRange(XSD.xstring);
aPourPrenom = model.createDatatypeProperty(uriBase+"aPourPrenom");
aPourPrenom.setDomain(personne);
aPourPrenom.setRange(XSD.xstring);
aDateNaiss = model.createDatatypeProperty(uriBase+"aDateNaiss");
aDateNaiss.setDomain(personne);
aDateNaiss.setRange(XSD.xstring);
}
}
public static void main(String args[])
{
new FamilyModel();
}
}
You have removed an item from a collection and then continued to use a previous iterator on it.
Most of the java collection iterators have the "fail fast" property. Once they detect that the underlying collection has changed they will throw a ConcurrentModificationException.
Your solution is to do a two step removal process:
iterate and find the nodes that you need to remove
iterate the list of nodes you want removed (found in first step) and actually remove them from the original collection.
Code (assuming Java 5):
//Button Remove
public class ActionRemove implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
List<Statement> statementsToRemove = new ArrayList<Statement>();
// step 1
StmtIterator iter = onto.model.listStatements();
while (iter.hasNext())
{
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property predicate = stmt.getPredicate();
RDFNode object = stmt.getObject();
if(subject.toString().equals (onto.uriBase+"#"+tabTF[0].getText()))
{
statementsToRemove.add(stmt);
}
}
// step 2
for( Statement stmt : statementsToRemove)
{
onto.model.remove(stmt);
}
}
}
There is another way (see http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CopyOnWriteArrayList.html class) but since your collection is inside the Jena api you can't use it ;).

adjust selected File to FileFilter in a JFileChooser

I'm writing a diagram editor in java. This app has the option to export to various standard image formats such as .jpg, .png etc. When the user clicks File->Export, you get a JFileChooser which has a number of FileFilters in it, for .jpg, .png etc.
Now here is my question:
Is there a way to have the extension of the default adjust to the selected file filter? E.g. if the document is named "lolcat" then the default option should be "lolcat.png" when the png filter is selected, and when the user selects the jpg file filter, the default should change to "lolcat.jpg" automatically.
Is this possible? How can I do it?
edit:
Based on the answer below, I wrote some code. But it doesn't quite work yet. I've added a propertyChangeListener to the FILE_FILTER_CHANGED_PROPERTY, but it seems that within this method getSelectedFile() returns null. Here is the code.
package nl.helixsoft;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
public class JFileChooserTest {
public class SimpleFileFilter extends FileFilter {
private String desc;
private List<String> extensions;
private boolean showDirectories;
/**
* #param name example: "Data files"
* #param glob example: "*.txt|*.csv"
*/
public SimpleFileFilter (String name, String globs) {
extensions = new ArrayList<String>();
for (String glob : globs.split("\\|")) {
if (!glob.startsWith("*."))
throw new IllegalArgumentException("expected list of globs like \"*.txt|*.csv\"");
// cut off "*"
// store only lower case (make comparison case insensitive)
extensions.add (glob.substring(1).toLowerCase());
}
desc = name + " (" + globs + ")";
}
public SimpleFileFilter(String name, String globs, boolean showDirectories) {
this(name, globs);
this.showDirectories = showDirectories;
}
#Override
public boolean accept(File file) {
if(showDirectories && file.isDirectory()) {
return true;
}
String fileName = file.toString().toLowerCase();
for (String extension : extensions) {
if (fileName.endsWith (extension)) {
return true;
}
}
return false;
}
#Override
public String getDescription() {
return desc;
}
/**
* #return includes '.'
*/
public String getFirstExtension() {
return extensions.get(0);
}
}
void export() {
String documentTitle = "lolcat";
final JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("Export");
jfc.setDialogType(JFileChooser.SAVE_DIALOG);
jfc.setSelectedFile(new File (documentTitle));
jfc.addChoosableFileFilter(new SimpleFileFilter("JPEG", "*.jpg"));
jfc.addChoosableFileFilter(new SimpleFileFilter("PNG", "*.png"));
jfc.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent arg0) {
System.out.println ("Property changed");
String extold = null;
String extnew = null;
if (arg0.getOldValue() == null || !(arg0.getOldValue() instanceof SimpleFileFilter)) return;
if (arg0.getNewValue() == null || !(arg0.getNewValue() instanceof SimpleFileFilter)) return;
SimpleFileFilter oldValue = ((SimpleFileFilter)arg0.getOldValue());
SimpleFileFilter newValue = ((SimpleFileFilter)arg0.getNewValue());
extold = oldValue.getFirstExtension();
extnew = newValue.getFirstExtension();
String filename = "" + jfc.getSelectedFile();
System.out.println ("file: " + filename + " old: " + extold + ", new: " + extnew);
if (filename.endsWith(extold)) {
filename.replace(extold, extnew);
} else {
filename += extnew;
}
jfc.setSelectedFile(new File (filename));
}
});
jfc.showDialog(frame, "export");
}
JFrame frame;
void run() {
frame = new JFrame();
JButton btn = new JButton ("export");
frame.add (btn);
btn.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent ae) {
export();
}
});
frame.setSize (300, 300);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFileChooserTest x = new JFileChooserTest();
x.run();
}
});
}
}
It looks like you can listen to the JFileChooser for a change on the FILE_FILTER_CHANGED_PROPERTY property, then change the extension of the selected file appropriately using setSelectedFile().
EDIT: You're right, this solution doesn't work. It turns out that when the file filter is changed, the selected file is removed if its file type doesn't match the new filter. That's why you're getting the null when you try to getSelectedFile().
Have you considered adding the extension later? When I am writing a JFileChooser, I usually add the extension after the user has chosen a file to use and clicked "Save":
if (result == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
String path = file.getAbsolutePath();
String extension = getExtensionForFilter(fileChooser.getFileFilter());
if(!path.endsWith(extension))
{
file = new File(path + extension);
}
}
fileChooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
FileFilter filter = (FileFilter)evt.getNewValue();
String extension = getExtensionForFilter(filter); //write this method or some equivalent
File selectedFile = fileChooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
path.substring(0, path.lastIndexOf("."));
fileChooser.setSelectedFile(new File(path + extension));
}
});
How about this:
class MyFileChooser extends JFileChooser {
public void setFileFilter(FileFilter filter) {
super.setFileFilter(filter);
FileChooserUI ui = getUI();
if( ui instanceof BasicFileChooserUI ) {
BasicFileChooserUI bui = (BasicFileChooserUI) ui;
String file = bui.getFileName();
if( file != null ) {
String newFileName = ... change extension
bui.setFileName( newFileName );
}
}
}
}
You can also use a PropertyChangeListener on the SELECTED_FILE_CHANGED_PROPERTY prior to attaching your suffix. When the selected file gets checked against the new filter (and subsequently set to null), the SELECTED_FILE_CHANGED_PROPERTY event is actually fired before the FILE_FILTER_CHANGED_PROPERTY event.
If the evt.getOldValue() != null and the evt.getNewValue() == null, you know that the JFileChooser has blasted your file. You can then grab the old file's name (using ((File)evt.getOldValue()).getName() as described above), pull off the extension using standard string parsing functions, and stash it into a named member variable within your class.
That way, when the FILE_FILTER_CHANGED event is triggered (immediately afterwards, as near as I can determine), you can pull that stashed root name from the named member variable, apply the extension for the new file filter type, and set the JFileChooser's selected file accordingly.
Here is my solution and it works fine. It maybe helps someone. You sould create your own "MyExtensionFileFilter" class, otherwise you have to modify the code.
public class MyFileChooser extends JFileChooser {
private File file = new File("");
public MyFileChooser() {
addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String filename = MyFileChooser.this.file.getName();
String extold = null;
String extnew = null;
if (e.getOldValue() == null || !(e.getOldValue() instanceof MyExtensionFileFilter)) {
return;
}
if (e.getNewValue() == null || !(e.getNewValue() instanceof MyExtensionFileFilter)) {
return;
}
MyExtensionFileFilter oldValue = ((MyExtensionFileFilter) e.getOldValue());
MyExtensionFileFilter newValue = ((MyExtensionFileFilter) e.getNewValue());
extold = oldValue.getExtension();
extnew = newValue.getExtension();
if (filename.endsWith(extold)) {
filename = filename.replace(extold, extnew);
} else {
filename += ("." + extnew);
}
setSelectedFile(new File(filename));
}
});
}
#Override
public void setSelectedFile(File file) {
super.setSelectedFile(file);
if(getDialogType() == SAVE_DIALOG) {
if(file != null) {
super.setSelectedFile(file);
this.file = file;
}
}
}
#Override
public void approveSelection() {
if(getDialogType() == SAVE_DIALOG) {
File f = getSelectedFile();
if (f.exists()) {
String msg = "File existes ...";
msg = MessageFormat.format(msg, new Object[] { f.getName() });
int option = JOptionPane.showConfirmDialog(this, msg, "", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.NO_OPTION ) {
return;
}
}
}
super.approveSelection();
}
#Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if(!visible) {
resetChoosableFileFilters();
}
}
}
Here is a method to obtain the current file name (as a String). In your property change listener for JFileChooser.FILE_FILTER_CHANGED_PROPERTY, you make the following call:
final JFileChooser fileChooser = new JFileChooser();
fileChooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener()
{
#Override
public void propertyChange(PropertyChangeEvent e) {
String currentName = ((BasicFileChooserUI)fileChooser.getUI()).getFileName();
MyFileFilter filter = (MyFileFilter) e.getNewValue();
// ... Transform currentName as you see fit using the newly selected filter.
// Suppose the result is in newName ...
fileChooser.setSelectedFile(new File(newName));
}
});
The getFileName() method of javax.swing.plaf.basic.BasicFileChooserUI (the descendant of FileChooserUI returned by JFileChooser.getUI()) will return the contents of the dialog's text box that is used to type in the file name. It seems that this value is always set to a non-null String (it returns an empty string if the box is empty). On the other hand, getSelectedFile() returns null if the user has not selected an existing file yet.
It seems that the dialog's design is governed by the 'file selection' concept; that is, while the dialog is visible getSelectedFile() only returns a meaningful value if the user has already selected an existing file or the program called setSelectedFile(). getSelectedFile() will return what the user typed in after the user clicks the approve (i.e. OK) button.
The technique will only work for single-selection dialogs, however changing file extension based on selected filter should also make sense for single files only ("Save As..." dialogs or similar).
This design was a subject of a debate at sun.com back in 2003, see the link for details.
The use of getAbsolutePath() in the previous change the current directory.
I was surprised when the JFileChooser dialog displaying "My documents" directory change to the Netbeans's project directory when I selected a different FileFilter, so I changed it to use getName().
I also used the JDK 6 FileNameExtensionFilter.
Here is the code:
final JFileChooser fc = new JFileChooser();
final File sFile = new File("test.xls");
fc.setSelectedFile(sFile);
// Store this filter in a variable to be able to select this after adding all FileFilter
// because addChoosableFileFilter add FileFilter in order in the combo box
final FileNameExtensionFilter excelFilter = new FileNameExtensionFilter("Excel document (*.xls)", "xls");
fc.addChoosableFileFilter(excelFilter);
fc.addChoosableFileFilter(new FileNameExtensionFilter("CSV document (*.csv)", "csv"));
// Force the excel filter
fc.setFileFilter(excelFilter);
// Disable All Files
fc.setAcceptAllFileFilterUsed(false);
// debug
fc.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Property name=" + evt.getPropertyName() + ", oldValue=" + evt.getOldValue() + ", newValue=" + evt.getNewValue());
System.out.println("getSelectedFile()=" + fc.getSelectedFile());
}
});
fc.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
Object o = evt.getNewValue();
if (o instanceof FileNameExtensionFilter) {
FileNameExtensionFilter filter = (FileNameExtensionFilter) o;
String ex = filter.getExtensions()[0];
File selectedFile = fc.getSelectedFile();
if (selectedFile == null) {
selectedFile = sFile;
}
String path = selectedFile.getName();
path = path.substring(0, path.lastIndexOf("."));
fc.setSelectedFile(new File(path + "." + ex));
}
}
});
Here's my attempt at this. It uses the accept() function to check whether or not the file passes the filter. If the filename does not, the extension is appended to the end.
JFileChooser jfc = new JFileChooser(getFile()) {
public void approveSelection() {
if (getDialogType() == SAVE_DIALOG) {
File selectedFile = getSelectedFile();
FileFilter ff = getFileFilter();
// Checks against the current selected filter
if (!ff.accept(selectedFile)) {
selectedFile = new File(selectedFile.getPath() + ".txt");
}
super.setSelectedFile(selectedFile);
if ((selectedFile != null) && selectedFile.exists()) {
int response = JOptionPane.showConfirmDialog(
this,
"The file " + selectedFile.getName() + " already exists.\n" +
"Do you want to replace it?",
"Ovewrite file",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
);
if (response == JOptionPane.NO_OPTION)
return;
}
}
super.approveSelection();
}
};
I recommend using FileNameExtensionFilter instead of FileFilter if your version of Java supports it. Otherwise create your own new similar abstract class that extends from FileFilter and has an added method getExtension (similar to FileNameExtensionFilter.getExtensions). Then override getExtension for every export filter you intend to use.
public abstract class MyFileFilter extends FileFilter {
abstract public String getExtension();
}
Then for an example JPG filter you just need to override one extra method than before:
MyFileFilter filterJPG = new MyFileFilter () {
#Override
public String getDescription() {
return "A JPEG image (*." + getExtension() + ")";
}
#Override
public boolean accept(File f) {
String filename = f.getName().toLowerCase();
return filename.endsWith("."+getExtension());
}
#Override
public String getExtension() { return "jpg"; }
};
After the user has chosen the file, simply call getFileFilter to determine which filter the user chose:
jfc.showDialog(frame, "export");
File file = jfc.getSelectedFile();
RvFileFilter filter = (RvFileFilter)jfc.getFileFilter();
String sExt = filter.getExtension(); //The extension to ensure the file has
If you were able to use FileNameExtensionFilter.getExtensions() then you can simply use the first entry in the array.

Categories

Resources