From: Bob J. <jac...@us...> - 2002-05-26 06:42:40
|
Update of /cvsroot/jmri/jmri/jmrit/roster In directory usw-pr-cvs1:/tmp/cvs-serv2736 Modified Files: CopyRosterItemAction.java Log Message: First really working revision Index: CopyRosterItemAction.java =================================================================== RCS file: /cvsroot/jmri/jmri/jmrit/roster/CopyRosterItemAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** CopyRosterItemAction.java 26 May 2002 00:43:18 -0000 1.1 --- CopyRosterItemAction.java 26 May 2002 06:42:37 -0000 1.2 *************** *** 35,80 **** Roster roster = Roster.instance(); ! // create a dialog to select the roster entry & capture the new name ! JComboBox selections = roster.matchingComboBox(null,null, null, null, null,null,null); ! JTextField newNameField = new JTextField(20); ! int retval = JOptionPane.showOptionDialog(null, ! "Select one roster entry", "Select roster entry", 0, JOptionPane.INFORMATION_MESSAGE, null, ! new Object[]{"Cancel", "OK", newNameField, selections}, null ); log.debug("Dialog value "+retval+" selected "+selections.getSelectedIndex()+":\"" ! +selections.getSelectedItem()+"\", \"" ! +newNameField.getText()+"\""); if (retval!=1) return; // user didn't select ! String entry = (String) selections.getSelectedItem(); ! String newEntry = newNameField.getText(); ! // find the file for the selected entry ! String filename = roster.fileFromTitle(entry); ! String fullFilename = XmlFile.prefsDir()+LocoFile.fileLocation+filename; ! log.debug("resolves to \""+filename+"\", \""+fullFilename+"\""); ! // read the file, change the ID, and write it out try { // ensure preferences will be found XmlFile.ensurePrefsPresent(XmlFile.prefsDir()+LocoFile.fileLocation); ! // locate the file and delete ! File f = new File(fullFilename); LocoFile lf = new LocoFile(); // used as a temporary Element lroot = null; try { ! lroot = lf.rootFromName(fullFilename); ! } catch (Exception e) { log.error("Exception while loading loco XML file: "+fullFilename+" exception: "+e); } } catch (Exception ex) { ! log.error("error during locomotive file output: "+ex); } - - // add new entry to roster - //roster.removeEntry(roster.entryFromTitle(entry)); - //roster.writeRosterFile(); - } --- 35,108 ---- Roster roster = Roster.instance(); ! // create a dialog to select the roster entry to copy ! JComboBox selections = roster.matchingComboBox(null, null, null, null, null, null, null); ! int retval = JOptionPane.showOptionDialog(mParent, ! "Select one roster entry to copy", "Select roster entry", 0, JOptionPane.INFORMATION_MESSAGE, null, ! new Object[]{"Cancel", "OK", selections}, null ); log.debug("Dialog value "+retval+" selected "+selections.getSelectedIndex()+":\"" ! +selections.getSelectedItem()+"\""); if (retval!=1) return; // user didn't select ! String fromID = (String) selections.getSelectedItem(); ! // find the file for the selected entry to copy ! RosterEntry fromEntry = roster.entryFromTitle(fromID); ! String fromFilename = roster.fileFromTitle(fromID); ! String fullFromFilename = LocoFile.fileLocation+fromFilename; ! log.debug("resolves to \""+fromFilename+"\", \""+fullFromFilename+"\""); ! ! String newEntryID = null; ! do { ! // prompt for the new ID ! newEntryID = JOptionPane.showInputDialog(mParent, "Enter id for new roster entry:"); ! if (newEntryID==null) return; ! ! // check for duplicate ! if (0 == roster.matchingList(null, null, null, null, null, null, newEntryID).size()) break; ! ! // here it is a duplicate, reprompt ! JOptionPane.showMessageDialog(mParent, ! "That entry already exists, please choose another"); ! } while (true); ! ! // read the input file, change the ID, and write it out try { // ensure preferences will be found XmlFile.ensurePrefsPresent(XmlFile.prefsDir()+LocoFile.fileLocation); ! // locate the file ! File f = new File(fullFromFilename); + // read it LocoFile lf = new LocoFile(); // used as a temporary Element lroot = null; try { ! lroot = lf.rootFromName(fullFromFilename); ! } catch (Exception e) { ! log.error("Exception while loading loco XML file: "+fullFromFilename+" exception: "+e); ! return; ! } ! ! // create a new entry ! RosterEntry newEntry = new RosterEntry(fromEntry, newEntryID); ! ! // set the filename from the ID ! newEntry.ensureFilenameExists(); ! ! // transfer the contents to a new file ! LocoFile newLocoFile = new LocoFile(); ! File fout = new File(XmlFile.prefsDir()+LocoFile.fileLocation+newEntry.getFileName()); ! newLocoFile.writeFile(fout, lroot, newEntry); ! ! // add the new entry to the roster & write it out ! roster.addEntry(newEntry); ! roster.writeRosterFile(); } catch (Exception ex) { ! log.error("unexpected error during copy operation: "+ex); ! return; } } |