use of org.jdom2.Parent in project JMRI by JMRI.
the class TabbedPreferences method init.
/**
* Initialize, including loading classes provided by a
* {@link java.util.ServiceLoader}.
* <p>
* Keeps a current state to prevent doing its work twice.
*
* @return The current state, which should be INITIALISED if all is well.
*/
@SuppressWarnings("rawtypes")
public synchronized int init() {
if (initialisationState == INITIALISED) {
return INITIALISED;
}
if (initialisationState != UNINITIALISED) {
return initialisationState;
}
this.setInitalisationState(INITIALISING);
list = new JList<>();
listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(100, 100));
buttonpanel = new JPanel();
buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.Y_AXIS));
buttonpanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 3));
detailpanel = new JPanel();
detailpanel.setLayout(new CardLayout());
detailpanel.setBorder(BorderFactory.createEmptyBorder(6, 3, 6, 6));
save = new JButton(ConfigBundle.getMessage("ButtonSave"), new ImageIcon(FileUtil.findURL("program:resources/icons/misc/gui3/SaveIcon.png", FileUtil.Location.INSTALLED)));
save.addActionListener((ActionEvent e) -> {
savePressed(invokeSaveOptions());
});
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
Set<PreferencesPanel> delayed = new HashSet<>();
for (PreferencesPanel panel : InstanceManager.getList(jmri.swing.PreferencesPanel.class)) {
if (panel instanceof PreferencesSubPanel) {
String parent = ((PreferencesSubPanel) panel).getParentClassName();
if (!this.getPreferencesPanels().containsKey(parent)) {
delayed.add(panel);
} else {
((PreferencesSubPanel) panel).setParent(this.getPreferencesPanels().get(parent));
}
}
if (!delayed.contains(panel)) {
this.addPreferencesPanel(panel);
}
}
for (PreferencesPanel panel : ServiceLoader.load(PreferencesPanel.class)) {
if (panel instanceof PreferencesSubPanel) {
String parent = ((PreferencesSubPanel) panel).getParentClassName();
if (!this.getPreferencesPanels().containsKey(parent)) {
delayed.add(panel);
} else {
((PreferencesSubPanel) panel).setParent(this.getPreferencesPanels().get(parent));
}
}
if (!delayed.contains(panel)) {
this.addPreferencesPanel(panel);
}
}
while (!delayed.isEmpty()) {
Set<PreferencesPanel> iterated = new HashSet<>(delayed);
iterated.stream().filter((panel) -> (panel instanceof PreferencesSubPanel)).forEach((panel) -> {
String parent = ((PreferencesSubPanel) panel).getParentClassName();
if (this.getPreferencesPanels().containsKey(parent)) {
((PreferencesSubPanel) panel).setParent(this.getPreferencesPanels().get(parent));
delayed.remove(panel);
this.addPreferencesPanel(panel);
}
});
}
preferencesArray.stream().forEach((preferences) -> {
detailpanel.add(preferences.getPanel(), preferences.getPrefItem());
});
updateJList();
add(buttonpanel);
add(new JSeparator(JSeparator.VERTICAL));
add(detailpanel);
list.setSelectedIndex(0);
selection(preferencesArray.get(0).getPrefItem());
this.setInitalisationState(INITIALISED);
return initialisationState;
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class WarrantPreferences method save.
public void save() {
if (_fileName == null) {
log.error("_fileName null. Could not create warrant preferences file.");
return;
}
XmlFile xmlFile = new XmlFile() {
};
xmlFile.makeBackupFile(_fileName);
File file = new File(_fileName);
try {
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (!parentDir.mkdir()) {
log.warn("Could not create parent directory for prefs file :{}", _fileName);
return;
}
}
if (file.createNewFile()) {
log.debug("Creating new warrant prefs file: {}", _fileName);
}
} catch (IOException ea) {
log.error("Could not create warrant preferences file at {}.", _fileName, ea);
}
try {
Element root = new Element("warrantPreferences");
Document doc = XmlFile.newDocument(root);
if (store(root)) {
xmlFile.writeXML(file, doc);
}
} catch (IOException eb) {
log.warn("Exception in storing warrant xml: {}", eb);
}
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class ThrottleFrame method saveThrottle.
private void saveThrottle(String sfile) {
// Save throttle: title / window position
// as strongly linked to extended throttles and roster presence, do not save function buttons and background window as they're stored in the roster entry
XmlFile xf = new XmlFile() {
};
// odd syntax is due to XmlFile being abstract
xf.makeBackupFile(sfile);
File file = new File(sfile);
try {
//The file does not exist, create it before writing
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (// make directory and check result
!parentDir.mkdir()) {
log.error("could not make parent directory");
}
}
if (// create file, check success
!file.createNewFile()) {
log.error("createNewFile failed");
}
} catch (Exception exp) {
log.error("Exception while writing the throttle file, may not be complete: " + exp);
}
try {
Element root = new Element("throttle-config");
Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttle-config.dtd");
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
/* java.util.Map<String,String> m = new java.util.HashMap<String,String>();
m.put("type", "text/xsl");
m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttle.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0,p);*/
Element throttleElement = getXml();
// throttleElement.getChild("AddressPanel").removeChild("locoaddress");
if (// don't save function buttons labels, they're in roster entry
(this.getRosterEntry() != null) && (getDefaultThrottleFolder() + addressPanel.getRosterEntry().getId().trim() + ".xml").compareTo(sfile) == 0) {
throttleElement.getChild("FunctionPanel").removeChildren("FunctionButton");
}
root.setContent(throttleElement);
xf.writeXML(file, doc);
setLastUsedSaveFile(sfile);
} catch (Exception ex) {
log.warn("Exception while storing throttle xml: " + ex);
}
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class ThrottlesPreferences method save.
public void save() {
if (prefFile == null) {
return;
}
XmlFile xf = new XmlFile() {
};
// odd syntax is due to XmlFile being abstract
xf.makeBackupFile(prefFile);
File file = new File(prefFile);
try {
//The file does not exist, create it before writing
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (// make directory, check result
!parentDir.mkdir()) {
log.error("failed to make parent directory");
}
}
if (// create file, check result
!file.createNewFile()) {
log.error("createNewFile failed");
}
} catch (Exception exp) {
log.error("Exception while writing the new throttles preferences file, may not be complete: " + exp);
}
try {
Element root = new Element("throttles-preferences");
Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttles-preferences.dtd");
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
/*TODO java.util.Map<String,String> m = new java.util.HashMap<String,String>();
m.put("type", "text/xsl");
m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttles-preferences.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0,p);*/
root.setContent(store());
xf.writeXML(file, doc);
} catch (Exception ex) {
log.warn("Exception in storing throttles preferences xml: " + ex);
}
this.dirty = false;
}
use of org.jdom2.Parent in project JMRI by JMRI.
the class AbstractWiThrottlePreferences method save.
public void save() {
if (fileName == null) {
return;
}
XmlFile xmlFile = new XmlFile() {
};
xmlFile.makeBackupFile(fileName);
File file = new File(fileName);
try {
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (!parentDir.mkdir()) {
log.warn("Could not create parent directory for prefs file :" + fileName);
return;
}
}
if (file.createNewFile()) {
log.debug("Creating new WiThrottle prefs file: " + fileName);
}
} catch (Exception ea) {
log.error("Could not create WiThrottle preferences file.");
}
try {
Element root = new Element("withrottle-prefs");
Document doc = XmlFile.newDocument(root);
root.setContent(store());
xmlFile.writeXML(file, doc);
} catch (Exception eb) {
log.warn("Exception in storing WiThrottle xml: " + eb);
}
}
Aggregations