📄 cbsaveloadtemplate.java
字号:
{
if (myContainer == null)
{
log.warning("Unexpected error in CBSaveLoadTemplate.save() - no parent found");
return;
} // should never happen
Component[] components = myContainer.getComponents();
for (int i = 0; i < components.length; i++)
{
Component c = components[i];
saveComponent(c, i, templateName);
}
}
/**
* This method handles the saving of a particular component. If
* the class needs to be extended, extra handling for particular
* components may need to be added, in which case this method
* can be extended.
*
* @param c the component to save
* @param componentNo the index of the component
* @param templateName the name of the template being saved to.
*/
protected void saveComponent(Component c, int componentNo, String templateName)
{
if ((c instanceof JPanel) && (c != this)) // if it's a container (and not this component), recurse and save those...
{
saveContainerInfo((Container) c, templateName + "." + componentNo);
}
else if ((c instanceof JScrollPane) && (c != this))
{
saveContainerInfo((Container) c, templateName + "." + componentNo);
}
else if ((c instanceof JViewport) && (c != this))
{
saveContainerInfo((Container) c, templateName + "." + componentNo);
}
else
{
String saveText = getComponentText(c);
if (saveText != null)
templates.setProperty(templateName + "." + componentNo, saveText);
}
}
/**
* this ftn returns the text to save for a given component, and
* is the sister ftn to 'loadComponentText'. New components
* (including awt components) can be added here, by
* extracting their value as text, and making sure that loadComponentText
* correctly translates that value when loading.
*
* @param c the component to retrieve a text value for.
* @return the text value of the component.
*/
public String getComponentText(Component c)
{
if (c == null) return null; // don't get null components
if (illegalComponents.contains(c)) return null; // don't get forbidden components.
try
{
if (c instanceof JPasswordField) // don't save passwords
return "";
else if (c instanceof JTextField)
return ((JTextField) c).getText();
else if (c instanceof JTextArea)
return ((JTextArea) c).getText();
else if (c instanceof TextField)
return ((TextField) c).getText();
else if (c instanceof JToggleButton)
return String.valueOf(((JToggleButton) c).isSelected());
else if (c instanceof CBJComboBox)
return ((CBJComboBox) c).getSelectedItem().toString();
else
return null; // unknown component
}
catch (Exception e)
{
return null;
} // possibility of uninitialised objects above...may cause problems.
}
/**
* Takes a template name, and attempts to read all the (string)
* values belonging to that template, and toss them in the
* appropriate numbered component.
*/
public void load()
{
String templateName = getCurrentTemplateName();
if (templateName.length() == 0)
{
CBUtility.error(this, CBIntText.get("No template selected!"), null);
return;
}
loadTemplateName(templateName);
}
public void loadTemplateName(String templateName)
{
Container parent = getParent();
loadContainerInfo(parent, templateName);
}
public void loadContainerInfo(Container myContainer, String templateName)
{
if (myContainer == null)
{
log.warning("Unexpected error in CBSaveLoadTemplate.load() - no parent found");
return;
} // should never happen
Component[] components = myContainer.getComponents();
for (int i = 0; i < components.length; i++)
{
Component c = components[i];
loadComponent(c, i, templateName);
}
}
/**
* This method handles the loading of a particular component. If
* the class needs to be extended, extra handling for particular
* components may need to be added, in which case this method
* can be extended.
*
* @param c the component to load
* @param componentNo the index of the component
* @param templateName the name of the template being loaded from.
*/
protected void loadComponent(Component c, int componentNo, String templateName)
{
if (c instanceof JPanel) // recurse into container details.
{
loadContainerInfo((Container) c, templateName + "." + componentNo);
}
else if ((c instanceof JScrollPane))
{
loadContainerInfo((Container) c, templateName + "." + componentNo);
}
else if ((c instanceof JViewport))
{
loadContainerInfo((Container) c, templateName + "." + componentNo);
}
else
{
String text = (String) templates.get(templateName + "." + componentNo); // often there won't be a value...
if (text != null) // ... if there is, load the data up!
{
loadComponentText(c, text);
}
}
}
public void loadComponentText(Component c, String text)
{
if (illegalComponents.contains(c)) return; // don't load forbidden components.
if (c instanceof JTextField)
((JTextField) c).setText(text);
else if (c instanceof JTextArea)
((JTextArea) c).setText(text);
else if (c instanceof TextField)
((TextField) c).setText(text);
else if (c instanceof JToggleButton)
((JToggleButton) c).setSelected("true".equalsIgnoreCase(text));
else if (c instanceof CBJComboBox)
{
((CBJComboBox) c).setSelectedItem(text);
}
}
public void delete()
{
String templateName = getCurrentTemplateName();
if (templateName.length() == 0)
{
CBUtility.error(this, "No template selected!", null);
return;
}
Container parent = getParent();
if (parent == null)
{
log.warning("Unexpected error in CBSaveLoadTemplate.delete() - no parent found");
return;
} // should never happen
deleteComponentInfo(parent, templateName);
for (int i = 0; i < numTemplates; i++)
{
if (templateName.equals((String) templates.get(TEMPLATENAME + i)))
{
templates.remove(TEMPLATENAME + i);
numTemplates--;
templates.put(NUMTEMPLATES, Integer.toString(numTemplates));
loadops.removeItem(templateName);
// and shuffle down all the values from above!
// (you know, there's got to be a neater way of doing this...)
for (int j = i + 1; j <= numTemplates; j++)
{
templateName = (String) templates.get(TEMPLATENAME + j);
templates.put(TEMPLATENAME + (j - 1), templateName);
}
templates.remove(TEMPLATENAME + numTemplates);
break;
}
}
CBUtility.writePropertyFile(configFile, templates, null);
}
public void deleteComponentInfo(Container myContainer, String templateName)
{
Component[] components = myContainer.getComponents();
// Brute force delete - most of these don't exist, but attempting deletion does no harm...
for (int i = 0; i < components.length; i++)
{
if (components[i] instanceof JPanel)
deleteComponentInfo((Container) components[i], templateName + "." + i);
else if ((components[i] instanceof JScrollPane))
deleteComponentInfo((Container) components[i], templateName + "." + i);
else if ((components[i] instanceof JViewport))
deleteComponentInfo((Container) components[i], templateName + "." + i);
else
deleteComponentInfo(templateName + "." + i);//templates.remove(templateName + "." + i);
}
try
{ //TE: if the default template is being deleted, also delete the 'defaul' entry in the property file.
if (templates.getProperty("default") != null && templates.getProperty("default").equalsIgnoreCase(templateName))
templates.remove("default");
}
catch (Exception e)
{
log.log(Level.WARNING, "No default template. ", e);
}
}
/**
* Deletes a template that is saved in the property file. Can be extended
* to account for individual component handling e.g. combo box visibility.
*
* @param templateName the name of the template that is to be deleted from the property file.
*/
public void deleteComponentInfo(String templateName)
{
templates.remove(templateName);
}
public void makeDefault()
{
templates.setProperty(DEFAULT, getCurrentTemplateName());
CBUtility.writePropertyFile(configFile, templates, "");
}
public void addIllegalComponent(Component c)
{
illegalComponents.add(c);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -