/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* JPanelAll.java
*
* Created on 27.04.2009, 22:13:10
*/
package xplanetconfigurator.gui;
import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.text.JTextComponent;
import xplanetconfigurator.util.OwnPreferences;
/**
*
* @author tom
*/
public class JPanelControlDownloaderSatellites extends ControlPanel implements IAskForUserTextLine {
private static final String GUI_DIVIDER_LOCATION_HORIZONTAL = "satellites.gui_divider_location_horizontal";
private Logger logger;
private List markerPanels;
int markerCountGridY = 1;
boolean addMarker;
boolean isAddingPixelForAlignmentRefresh;
private String downloadIntervallTmpForNewMarkers;
/** Creates new form JPanelAll */
public JPanelControlDownloaderSatellites(MainFrame mainFrame) {
super(mainFrame);
initComponents();
this.logger = Logger.getLogger(this.getClass().getName());
this.markerPanels = new ArrayList();
this.loadUserPrefs();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jSplitPaneMain = new javax.swing.JSplitPane();
jPanelControls = new javax.swing.JPanel();
jPanelWait = new javax.swing.JPanel();
jLabelWait = new javax.swing.JLabel();
jTextFieldWait = new javax.swing.JTextField();
jLabelMinutes = new javax.swing.JLabel();
jPanelMarkersAndButtons = new javax.swing.JPanel();
jPanelAddRemove = new javax.swing.JPanel();
jButtonAdd = new javax.swing.JButton();
jButtonRemove = new javax.swing.JButton();
jScrollPanelMarkers = new javax.swing.JScrollPane();
jPanelMarkers = new javax.swing.JPanel();
jPanelPlaceholder = new javax.swing.JPanel();
jPanelHelp = new javax.swing.JPanel();
jLabelHelp = new javax.swing.JLabel();
jScrollPane = new javax.swing.JScrollPane();
jTextAreaHelp = new javax.swing.JTextArea();
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentHidden(java.awt.event.ComponentEvent evt) {
formComponentHidden(evt);
}
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
setLayout(new java.awt.GridBagLayout());
jSplitPaneMain.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jSplitPaneMain.setName("jSplitPaneMain"); // NOI18N
jPanelControls.setName("jPanelControls"); // NOI18N
jPanelControls.setLayout(new java.awt.GridBagLayout());
jPanelWait.setName("jPanelWait"); // NOI18N
jPanelWait.setLayout(new java.awt.GridBagLayout());
jLabelWait.setText("Download Intervall"); // NOI18N
jLabelWait.setName("jLabelWait"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
jPanelWait.add(jLabelWait, gridBagConstraints);
jTextFieldWait.setMinimumSize(new java.awt.Dimension(70, 20));
jTextFieldWait.setName("jTextFieldWait"); // NOI18N
jTextFieldWait.setPreferredSize(new java.awt.Dimension(70, 20));
jTextFieldWait.addCaretListener(new javax.swing.event.CaretListener() {
public void caretUpdate(javax.swing.event.CaretEvent evt) {
jTextFieldWaitCaretUpdate(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
jPanelWait.add(jTextFieldWait, gridBagConstraints);
jLabelMinutes.setText("minutes");
jLabelMinutes.setName("jLabelMinutes"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
jPanelWait.add(jLabelMinutes, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
jPanelControls.add(jPanelWait, gridBagConstraints);
jPanelMarkersAndButtons.setBorder(javax.swing.BorderFactory.createTitledBorder("Satellites"));
jPanelMarkersAndButtons.setName("jPanelMarkersAndButtons"); // NOI18N
jPanelMarkersAndButtons.setLayout(new java.awt.GridBagLayout());
jPanelAddRemove.setName("jPanelAddRemove"); // NOI18N
jPanelAddRemove.setLayout(new java.awt.GridBagLayout());
jButtonAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xplanetconfigurator/gui/resources/img/Button-Add-16x16.gif"))); // NOI18N
jButtonAdd.setToolTipText("Add Marker");
jButtonAdd.setName("jButtonAdd"); // NOI18N
jButtonAdd.setPreferredSize(new java.awt.Dimension(25, 25));
jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAddActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
jPanelAddRemove.add(jButtonAdd, gridBagConstraints);
jButtonRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xplanetconfigurator/gui/resources/img/Button-Delete-16x16.gif"))); // NOI18N
jButtonRemove.setToolTipText("Remove Marker");
jButtonRemove.setName("jButtonRemove"); // NOI18N
jButtonRemove.setPreferredSize(new java.awt.Dimension(25, 25));
jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonRemoveActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
jPanelAddRemove.add(jButtonRemove, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
jPanelMarkersAndButtons.add(jPanelAddRemove, gridBagConstraints);
jScrollPanelMarkers.setMinimumSize(new java.awt.Dimension(23, 120));
jScrollPanelMarkers.setName("jScrollPanelMarkers"); // NOI18N
jScrollPanelMarkers.setPreferredSize(new java.awt.Dimension(23, 120));
jPanelMarkers.setName("jPanelMarkers"); // NOI18N
jPanelMarkers.setLayout(new java.awt.GridBagLayout());
jPanelPlaceholder.setName("jPanelPlaceholder"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 99;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
jPanelMarkers.add(jPanelPlaceholder, gridBagConstraints);
jScrollPanelMarkers.setViewportView(jPanelMarkers);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
jPanelMarkersAndButtons.add(jScrollPanelMarkers, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanelControls.add(jPanelMarkersAndButtons, gridBagConstraints);
jSplitPaneMain.setLeftComponent(jPanelControls);
jPanelHelp.setName("jPanelHelp"); // NOI18N
jPanelHelp.setLayout(new java.awt.GridBagLayout());
jLabelHelp.setText("Help Text");
jLabelHelp.setName("jLabelHelp"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
jPanelHelp.add(jLabelHelp, gridBagConstraints);
jScrollPane.setName("jScrollPane"); // NOI18N
jTextAreaHelp.setColumns(20);
jTextAreaHelp.setEditable(false);
jTextAreaHelp.setRows(5);
jTextAreaHelp.setText("Download Intervall\nIs in minutes. If the text field is empty the downloader takes 180 minutes\n(3 hours) as default. Applies for all satellite files.\n\nAdd/Remove a Marker\n- To add a sattelite file press the upper \"+\" button. Type a name in the dialog.\nAdd a URL to the combo box. Example:\nname:\tiss\nURL:\thttp://www.wizabit.eclipse.co.uk/xplanet/files/local/iss\nname:\tiss_tle\nURL:\thttp://www.wizabit.eclipse.co.uk/xplanet/files/local/iss.tle\n- To remove a marker press the upper \"-\" button.\n\nRemember to add a pair of files per satellite, Example:\n- iss\n- iss.tle\n\nRemember this is the download only. If you want o see the markers you\nhave to set at least the parameter \"satellite_file\" in the config file.\nPlease refer to the XPlanet parameters below that are related to the clouds.\n\n\n---XPlanet Parameters in Config File---\n\nsatellite_file\nSpecify a file containing a list of satellites to display. A file\ncontaining NORAD two line element (TLE) sets named satfile.tle must\nexist along with satfile. A good source of TLEs is www.celestrak.com.\nThis option may be used more than once. The default is no satellite\nfiles. See the README and sample files in the xplanet/satellites\ndirectory for more information."); // NOI18N
jTextAreaHelp.setName("jTextAreaHelp"); // NOI18N
jScrollPane.setViewportView(jTextAreaHelp);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanelHelp.add(jScrollPane, gridBagConstraints);
jSplitPaneMain.setRightComponent(jPanelHelp);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(jSplitPaneMain, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
this.receiveCommandLineParameters();
}//GEN-LAST:event_formComponentShown
private void jTextFieldWaitCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextFieldWaitCaretUpdate
if (this.jTextFieldWait.hasFocus()) {
this.sendDownloadParamterWait();
}
}//GEN-LAST:event_jTextFieldWaitCaretUpdate
private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
this.addMarker = true;
JDialogTextLineInput dialog = new JDialogTextLineInput("Add a Satellite. Please give a Name", this);
dialog.setVisible(true);
}//GEN-LAST:event_jButtonAddActionPerformed
private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
this.addMarker = false;
JDialogTextLineInput dialog = new JDialogTextLineInput("Remove a Satellite. Please give a Name", this);
dialog.setVisible(true);
}//GEN-LAST:event_jButtonRemoveActionPerformed
private void formComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentHidden
this.saveUserPrefs();
}//GEN-LAST:event_formComponentHidden
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonAdd;
private javax.swing.JButton jButtonRemove;
private javax.swing.JLabel jLabelHelp;
private javax.swing.JLabel jLabelMinutes;
private javax.swing.JLabel jLabelWait;
private javax.swing.JPanel jPanelAddRemove;
private javax.swing.JPanel jPanelControls;
private javax.swing.JPanel jPanelHelp;
private javax.swing.JPanel jPanelMarkers;
private javax.swing.JPanel jPanelMarkersAndButtons;
private javax.swing.JPanel jPanelPlaceholder;
private javax.swing.JPanel jPanelWait;
private javax.swing.JScrollPane jScrollPane;
private javax.swing.JScrollPane jScrollPanelMarkers;
private javax.swing.JSplitPane jSplitPaneMain;
private javax.swing.JTextArea jTextAreaHelp;
private javax.swing.JTextField jTextFieldWait;
// End of variables declaration//GEN-END:variables
/**
* Analyse the configuration of the downloader and set the values of
* the markers accordingly.
*
* @param parameters
* @return
*/
@Override
boolean receiveCommandLineParameters(String parameters) {
// Update the Status
String params = this.getMainFrame().getDownloaderConfig();
// TODO: Call all markers to reload its values (status of local and downloaded file,...)
String regExpr = "(?i)(satellites\\.\\w+.wait=)(\\S+)";
String found = this.receiveParameter(params, regExpr, this.jTextFieldWait, 2);
this.checkMarkerPanels();
// TODO: Call all markers
Iterator it = this.markerPanels.iterator();
while (it.hasNext()) {
JPanelControlDownloaderSatellite markerPanel = (JPanelControlDownloaderSatellite) it.next();
markerPanel.receiveCommandLineParameters(parameters);
}
/*
this.removeAllMarkerPanels();
this.addMarkerPanels();
* */
if (found != null) {
this.downloadIntervallTmpForNewMarkers = found;
return true;
} else {
this.downloadIntervallTmpForNewMarkers = "180";
this.jTextFieldWait.setText("180");
return false;
}
}
private void sendDownloadParamterWait() {
Iterator it = this.markerPanels.iterator();
while (it.hasNext()) {
JPanelControlDownloaderSatellite markerPanel = (JPanelControlDownloaderSatellite) it.next();
this.sendDownloadParameter(
this.jTextFieldWait,
"(satellites\\." + markerPanel.getMarkerName() + "\\.wait=)(.*)",
"satellites." + markerPanel.getMarkerName() + ".wait");
this.sendDownloadParameter(
this.jTextFieldWait,
"(satellites\\." + markerPanel.getMarkerName() + "tle\\.wait=)(.*)",
"satellites." + markerPanel.getMarkerName() + "tle.wait");
}
}
public String getText() {
return this.jTextAreaHelp.getText();
}
public JTextComponent getTextComponent() {
return this.jTextAreaHelp;
}
@Override
public boolean isDownloaderControlPanel() {
return true;
}
private void checkMarkerPanels() {
List markerNamesInDownloadConfig = this.readAllMarkerPanesFromDownloaderConfiguration("(?i)(?m)^(satellites\\.)(\\w+)");
List markerNamesInGUI = this.getAllMarkerPanelsShownInGUI();
// Remove MarkerPanels from the GUI that are not in the config any longer
Iterator it = markerNamesInGUI.iterator();
while(it.hasNext()) {
String markerNameGUI = (String) it.next();
if(! markerNamesInDownloadConfig.contains(markerNameGUI)) {
// Remove from GUI. (Tries to remove it from config as well although it not there.)
this.removeMarkerPanel(markerNameGUI);
}
}
// Add MarkerPanels that are missing in the GUI but not in the config.
it = markerNamesInDownloadConfig.iterator();
while(it.hasNext()) {
String markerNameConfig = (String) it.next();
if(! markerNamesInGUI.contains(markerNameConfig)) {
// Remove from GUI. (Tries to remove it from config as well although it not there.)
this.addMarkerPanel(markerNameConfig);
}
}
}
/**
*
* @return a List of Strings containing all marker names shown in the GUI
*/
private List getAllMarkerPanelsShownInGUI() {
List tempListMarkerNames = new ArrayList();
Iterator it = this.markerPanels.iterator();
while (it.hasNext()) {
JPanelControlDownloaderSatellite markerPanel = (JPanelControlDownloaderSatellite) it.next();
String s = markerPanel.getMarkerName();
tempListMarkerNames.add(s);
}
return tempListMarkerNames;
}
/**
* Called at intial load depending on the Uploader (config of uploader)
*/
private void addMarkerPanels() {
List names = this.readAllMarkerPanesFromDownloaderConfiguration("(?i)(?m)^(satellites\\.)(\\w+)");
Iterator it = names.iterator();
while (it.hasNext()) {
String panelName = (String) it.next();
this.addMarkerPanel(panelName);
}
}
/**
* Called at intial load depending on the Uploader (config of uploader)
* and if the user adds a markerPanel by pressing the add button
*/
private void addMarkerPanel(String satelliteName) {
JPanelControlDownloaderSatellite markerPanel = new JPanelControlDownloaderSatellite(this.getMainFrame(), satelliteName, this);
this.markerPanels.add(markerPanel);
GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = this.markerCountGridY++;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
this.jPanelMarkers.add(markerPanel, gridBagConstraints);
// Import for aligning the markers. It not called the label to allign
// has 0 with and hight and will not be visible.
this.jPanelMarkers.validate();
}
private void removeMarkerPanel(String markerNameToRemove) {
Iterator it = this.markerPanels.iterator();
while (it.hasNext()) {
JPanelControlDownloaderSatellite markerPanel = (JPanelControlDownloaderSatellite) it.next();
String s = markerPanel.getMarkerName();
if (s.equalsIgnoreCase(markerNameToRemove)) {
this.jPanelMarkers.remove(markerPanel);
this.markerPanels.remove(markerPanel);
this.removeMarkerFromUplaoder(s, "satellites");
this.removeMarkerFromUplaoder(s + "tle", "satellites");
this.jPanelMarkers.validate();
return;
}
}
}
/**
* Add or remove a markerPanel after the user opened a Dialog.
* @param nameFromDialog
*/
public void receiveUserTextLine(String nameFromDialog) {
// do not allow blanks
nameFromDialog = nameFromDialog.replaceAll("\\s+", "");
if (nameFromDialog.equals("")) {
//Do nothing
return;
}
nameFromDialog = nameFromDialog.toLowerCase();
// Check wether to remove markerPanel
if (!this.addMarker) {
this.removeMarkerPanel(nameFromDialog);
this.updateConfiguration();
return;
}
// Add markerPanel
// Check wether other satellites have the same name nameFromDialog exists already
Iterator it = this.markerPanels.iterator();
while (it.hasNext()) {
JPanelControlDownloaderSatellite marker = (JPanelControlDownloaderSatellite) it.next();
String s = marker.getMarkerName();
if (s.equalsIgnoreCase(nameFromDialog)) {
this.popError("A marker of this name does exits.\nChoose another name.");
return;
}
}
// Check the whole downloader config for double names
boolean exists = this.checkIfNameOfMarkerExists(nameFromDialog, "satellites");
if (exists) {
this.popError("A marker or satellite named like this does exits.\nChoose another name.");
return;
}
this.addMarkerPanel(nameFromDialog);
this.jPanelMarkers.validate();
this.updateConfiguration();
}
private void loadUserPrefs() {
logger.finer("Getting user preferences...");
OwnPreferences prefs = OwnPreferences.userNodeForPackage(this.getClass()); // Position an size of frame
int i = prefs.getInt(JPanelControlDownloaderSatellites.GUI_DIVIDER_LOCATION_HORIZONTAL, 300);
this.jSplitPaneMain.setDividerLocation(i);
}
private void saveUserPrefs() {
OwnPreferences prefs = OwnPreferences.userNodeForPackage(this.getClass());
prefs.putInt(JPanelControlDownloaderSatellites.GUI_DIVIDER_LOCATION_HORIZONTAL, this.jSplitPaneMain.getDividerLocation());
}
/**
* Updates the section (earth, moon,....) in the configuration by
* adding/changing/removing the marker_file for the section.
*/
public void updateConfiguration() {
Iterator it = this.markerPanels.iterator();
String keyValuePairs = "";
while (it.hasNext()) {
JPanelControlDownloaderSatellite marker = (JPanelControlDownloaderSatellite) it.next();
String markerURL = marker.getCurrentURL();
String fileName = getFileNameOfURL(markerURL);
if (fileName == null) {
continue;
}
// Do not allow blanks
// fileName = fileName.replaceAll("\\s+", "");
if (markerURL != null) {
if (keyValuePairs.length() > 0) {
keyValuePairs = keyValuePairs + "\n";
}
keyValuePairs = keyValuePairs + "satellite_file=" + fileName;
}
}
this.logger.finer("Update arc files of section default with key-value-pairs of '" + keyValuePairs + "'.");
sendConfigParameter("earth", keyValuePairs, "(?i)(satellite_file=)(\\S*)");
}
@Override
void receiveCommandLineParameters() {
String params = this.getMainFrame().getDownloaderConfig();
this.receiveCommandLineParameters(params);
}
}