[izpack-users] Clearing text when reload custom panel
Yossi Baram
yossiba at eldat.com
Tue Feb 28 08:52:19 CET 2006
Hi guys,
I have created a custom panel that do a validity check for memory space.
I run it right after the TargetPanel.
package com.izforge.izpack.panels;
import com.izforge.izpack.gui.ButtonFactory;
import com.izforge.izpack.gui.LabelFactory;
import com.izforge.izpack.installer.InstallData;
import com.izforge.izpack.installer.InstallerFrame;
import com.izforge.izpack.installer.IzPanel;
import com.izforge.izpack.util.VariableSubstitutor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
import java.io.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
// This class check available space for installation on Windows and
Linux as well
public class ValidityCheck extends IzPanel
{
private boolean EnoughFreeSpace = true;
/** The center panel. */
private JPanel centerPanel;
/** The variables substitutor. */
private VariableSubstitutor vs;
/** The layout. */
private BoxLayout layout;
public ValidityCheck(InstallerFrame parent, InstallData idata)
{
super(parent, idata);
vs = new VariableSubstitutor(idata.getVariables());
// The 'super' layout
GridBagLayout superLayout = new GridBagLayout();
setLayout(superLayout);
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.insets = new Insets(0, 0, 0, 0);
gbConstraints.fill = GridBagConstraints.NONE;
gbConstraints.anchor = GridBagConstraints.CENTER;
// We initialize our 'real' layout
centerPanel = new JPanel();
layout = new BoxLayout(centerPanel, BoxLayout.Y_AXIS);
centerPanel.setLayout(layout);
superLayout.addLayoutComponent(centerPanel, gbConstraints);
add(centerPanel);
}
public boolean isValidated()
{
return( EnoughFreeSpace );
}
public void panelActivate()
{
super.panelActivate();
try{
long minCapacity=3000; // 1.5 GB
centerPanel.add(LabelFactory.create(parent.langpack.getString("ValidityC
heck.head"),
null, JLabel.TRAILING));
centerPanel.add(Box.createVerticalStrut(20));
if(getFreeSpace(idata.getInstallPath().toString())/1000000<
minCapacity){
centerPanel.add(LabelFactory.create(parent.langpack.getString("ValidityC
heck.fail1"),
parent.icons.getImageIcon("information"), JLabel.TRAILING));
centerPanel.add(Box.createVerticalStrut(20));
centerPanel.add(LabelFactory.create(parent.langpack.getString("ValidityC
heck.fail2"),
parent.icons.getImageIcon("information"), JLabel.TRAILING));
centerPanel.add(Box.createVerticalStrut(20));
EnoughFreeSpace = false;
}
else{
centerPanel.add(LabelFactory.create(parent.langpack.getString("ValidityC
heck.success"),
parent.icons.getImageIcon("information"), JLabel.TRAILING));
centerPanel.add(Box.createVerticalStrut(20));
EnoughFreeSpace = true;
}
}
catch(Exception ex)
{
EnoughFreeSpace = false;
}
}
public long getFreeSpace(String path) throws Exception
{
if (System.getProperty("os.name").startsWith("Windows")) {
return getFreeSpaceOnWindows(path);
}
if (System.getProperty("os.name").startsWith("Linux")) {
return getFreeSpaceOnLinux(path);
}
throw new UnsupportedOperationException(
"The method getFreeSpace(String path) has not been implemented for
this operating system.");
}
private long getFreeSpaceOnWindows(String path) throws Exception
{
long bytesFree = -1;
File script = new File(System.getProperty("java.io.tmpdir"),
"script.bat");
PrintWriter writer = new PrintWriter(new FileWriter(script, false));
writer.println("dir \"" + path + "\"");
writer.close();
// get the output from running the .bat file
Process p = Runtime.getRuntime().exec(script.getAbsolutePath());
InputStream reader = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (; ; ) {
int c = reader.read();
if (c == -1)
break;
buffer.append( (char) c);
}
String outputText = buffer.toString();
reader.close();
// parse the output text for the bytes free info
StringTokenizer tokenizer = new StringTokenizer(outputText, "\n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// see if line contains the bytes free information
if (line.endsWith("bytes free")) {
tokenizer = new StringTokenizer(line, " ");
tokenizer.nextToken();
tokenizer.nextToken();
bytesFree = Long.parseLong(tokenizer.nextToken().replaceAll(
",", ""));
}
}
return bytesFree;
}
private long getFreeSpaceOnLinux(String path) throws Exception
{
long bytesFree = -1;
Process p = Runtime.getRuntime().exec("df " + "/" + path);
p.waitFor();
InputStream reader = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (; ; ) {
int c = reader.read();
if (c == -1)
break;
buffer.append( (char) c);
}
String outputText = buffer.toString();
reader.close();
// parse the output text for the bytes free info
StringTokenizer tokenizer = new StringTokenizer(outputText, "\n");
tokenizer.nextToken();
if(tokenizer.hasMoreTokens())
{
String line2 = tokenizer.nextToken();
StringTokenizer tokenizer2 = new StringTokenizer(line2, " ");
if(tokenizer2.countTokens()>=4)
{
tokenizer2.nextToken();
tokenizer2.nextToken();
tokenizer2.nextToken();
bytesFree = Long.parseLong(tokenizer2.nextToken());
return bytesFree;
}
return bytesFree;
}
throw new Exception("Can not read the free space of " + path + "
path");
}
}
It works great.
My problem here is, if the user get a message that he doesnt have enogh
space for installation he should return and set a different target
folder.
When the ValidityCheck panel reload the previous string remains and a
new one is added.
How can I make sure each time you go inside the panel, it has no past
strings that will be added to the new one?
Thanks
Yossi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.berlios.de/pipermail/izpack-users/attachments/20060228/436fa8ed/attachment.html
More information about the izpack-users
mailing list