From: Bill Kuker Date: Mon, 8 Nov 2010 01:49:54 +0000 (+0000) Subject: Move Summary to top of motor editor, make it show progress. X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=b73b9c25dcaa8eeb2835b3388f6b20df1b4b5bcc;p=sw%2Fmotorsim Move Summary to top of motor editor, make it show progress. Awesome --- diff --git a/gui/com/billkuker/rocketry/motorsim/visual/BurnPanel.java b/gui/com/billkuker/rocketry/motorsim/visual/BurnPanel.java index 6ee32df..e586d69 100644 --- a/gui/com/billkuker/rocketry/motorsim/visual/BurnPanel.java +++ b/gui/com/billkuker/rocketry/motorsim/visual/BurnPanel.java @@ -97,9 +97,6 @@ public class BurnPanel extends JPanel { add( new SL(), BorderLayout.SOUTH); BurnSummary bi = new BurnSummary(burn); - SummaryPanel text = new SummaryPanel(burn); - text.setBurnSummary(bi); - add(text, BorderLayout.NORTH); { diff --git a/gui/com/billkuker/rocketry/motorsim/visual/SummaryPanel.java b/gui/com/billkuker/rocketry/motorsim/visual/SummaryPanel.java index 3d7bb99..53f11d7 100644 --- a/gui/com/billkuker/rocketry/motorsim/visual/SummaryPanel.java +++ b/gui/com/billkuker/rocketry/motorsim/visual/SummaryPanel.java @@ -1,72 +1,110 @@ package com.billkuker.rocketry.motorsim.visual; +import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Dimension; import java.awt.GridLayout; import java.text.DecimalFormat; +import javax.measure.quantity.Length; +import javax.measure.unit.SI; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JProgressBar; +import javax.swing.SwingUtilities; + +import org.jscience.physics.amount.Amount; import com.billkuker.rocketry.motorsim.Burn; import com.billkuker.rocketry.motorsim.BurnSummary; import com.billkuker.rocketry.motorsim.RocketScience; -public class SummaryPanel extends JPanel { +public class SummaryPanel extends JPanel implements Burn.BurnProgressListener { private static final long serialVersionUID = 1L; private static final Color RED = new Color(196, 0, 0); private static final Color GREEN = new Color(0, 196, 0); private static final Color ORANGE = new Color(160, 96, 0); + private final Burn burn; + private final JProgressBar bar = new JProgressBar(); + public SummaryPanel(Burn b) { - super(new GridLayout(2, 5)); + setPreferredSize(new Dimension(100, 40)); + setLayout(new GridLayout(1, 1)); + bar.setStringPainted(true); + add(bar); + this.burn = b; + burn.addBurnProgressListener(this); + } + + @Override + public void setProgress(float p) { + int pct = (int) (p * 100); + bar.setValue(pct); + Amount web = burn.getMotor().getGrain().webThickness(); + Amount remaining = web.times(1.0 - p); + if ( remaining.isLessThan(Amount.valueOf(0, SI.MILLIMETER))){ + remaining = Amount.valueOf(0, remaining.getUnit()); + } + bar.setString("Burn Progress: " + pct + "% (" + RocketScience.ammountToRoundedString(remaining) + " web thickness remaining)"); + } + + @Override + public void burnComplete() { + setBurnSummary(new BurnSummary(burn)); } - public void setBurnSummary(BurnSummary bi) { - { - this.add(new JLabel("Rating")); - this.add(new JLabel("Total Impulse")); - this.add(new JLabel("ISP")); - this.add(new JLabel("Max Thrust")); - this.add(new JLabel("Average Thust")); - this.add(new JLabel("Max Pressure")); + private void setBurnSummary(final BurnSummary bi) { + + removeAll(); + setLayout(new GridLayout(2, 5)); + SwingUtilities.invokeLater(new Thread() { + public void run() { + SummaryPanel.this.add(new JLabel("Rating")); + SummaryPanel.this.add(new JLabel("Total Impulse")); + SummaryPanel.this.add(new JLabel("ISP")); + SummaryPanel.this.add(new JLabel("Max Thrust")); + SummaryPanel.this.add(new JLabel("Average Thust")); + SummaryPanel.this.add(new JLabel("Max Pressure")); - this.add(new JLabel("Safty Factor")); + SummaryPanel.this.add(new JLabel("Safty Factor")); - this.add(new JLabel(bi.getRating())); - this.add(new JLabel(RocketScience.ammountToRoundedString(bi - .totalImpulse()))); - this.add(new JLabel(RocketScience.ammountToRoundedString(bi - .specificImpulse()))); - this.add(new JLabel(RocketScience.ammountToRoundedString(bi - .maxThrust()))); - this.add(new JLabel(RocketScience.ammountToRoundedString(bi - .averageThrust()))); - this.add(new JLabel(RocketScience.ammountToRoundedString(bi - .maxPressure()))); + SummaryPanel.this.add(new JLabel(bi.getRating())); + SummaryPanel.this.add(new JLabel(RocketScience + .ammountToRoundedString(bi.totalImpulse()))); + SummaryPanel.this.add(new JLabel(RocketScience + .ammountToRoundedString(bi.specificImpulse()))); + SummaryPanel.this.add(new JLabel(RocketScience + .ammountToRoundedString(bi.maxThrust()))); + SummaryPanel.this.add(new JLabel(RocketScience + .ammountToRoundedString(bi.averageThrust()))); + SummaryPanel.this.add(new JLabel(RocketScience + .ammountToRoundedString(bi.maxPressure()))); - Color saftyColor; - if (bi.getSaftyFactor() == null) { + Color saftyColor; + if (bi.getSaftyFactor() == null) { - saftyColor = Color.BLACK; - this.add(new JLabel("NA")); - } else { - double d = bi.getSaftyFactor(); - if (d >= 1.5) { - saftyColor = GREEN; - } else if (d > 1) { - saftyColor = ORANGE; + saftyColor = Color.BLACK; + SummaryPanel.this.add(new JLabel("NA")); } else { - saftyColor = RED; + double d = bi.getSaftyFactor(); + if (d >= 1.5) { + saftyColor = GREEN; + } else if (d > 1) { + saftyColor = ORANGE; + } else { + saftyColor = RED; + } + JLabel l = new JLabel(new DecimalFormat("##########.#") + .format(bi.getSaftyFactor())); + l.setOpaque(true); + l.setBackground(saftyColor); + l.setForeground(Color.WHITE); + SummaryPanel.this.add(l); } - JLabel l = new JLabel( - new DecimalFormat("##########.#").format(bi - .getSaftyFactor())); - l.setOpaque(true); - l.setBackground(saftyColor); - l.setForeground(Color.WHITE); - this.add(l); + revalidate(); } - - } + }); } + } diff --git a/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java b/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java index d85780d..4954ebe 100644 --- a/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java +++ b/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java @@ -63,14 +63,18 @@ import com.billkuker.rocketry.motorsim.visual.BurnPanel; import com.billkuker.rocketry.motorsim.visual.Editor; import com.billkuker.rocketry.motorsim.visual.GrainPanel; import com.billkuker.rocketry.motorsim.visual.HardwarePanel; +import com.billkuker.rocketry.motorsim.visual.SummaryPanel; -public class MotorEditor extends JTabbedPane implements PropertyChangeListener { +public class MotorEditor extends JPanel implements PropertyChangeListener { private static final long serialVersionUID = 1L; private static Logger log = Logger.getLogger(MotorEditor.class); Motor motor; GrainEditor grainEditor; BurnTab bt; Burn burn; + SummaryPanel sp; + + JTabbedPane tabs; private Vector burnWatchers = new Vector(); private DefaultComboBoxModel availableFuels = new DefaultComboBoxModel(); @@ -151,10 +155,6 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener { currentThread = new Thread() { public void run() { final Thread me = this; - final JProgressBar bar = new JProgressBar(0, 100); - add(bar, BorderLayout.NORTH); - final JLabel progress = new JLabel(); - add(progress, BorderLayout.CENTER); try { final Burn b = new Burn(motor); b.addBurnProgressListener( @@ -163,37 +163,30 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener { public void burnComplete(){}; @Override public void setProgress(float f) { - int pct = (int)(f*100); - bar.setValue(pct); - Amount web = motor.getGrain().webThickness(); - Amount remaining = web.times(1.0 - f); - - progress.setText("Progress: " + pct + "% (" + RocketScience.ammountToRoundedString(remaining) + " web thickness remaining)"); if ( currentThread != me ){ throw new BurnCanceled(); } } }); + if ( sp != null ) + MotorEditor.this.remove(sp); + MotorEditor.this.add(sp = new SummaryPanel(b), BorderLayout.NORTH); + revalidate(); b.burn(); final BurnPanel bp = new BurnPanel(b); SwingUtilities.invokeLater(new Thread() { public void run() { - remove(bar); - remove(progress); add(bp, BorderLayout.CENTER); - for (BurnWatcher bw : burnWatchers) bw.replace(burn, b); burn = b; - revalidate(); } }); } catch (BurnCanceled c){ log.info("Burn Canceled!"); } catch (Exception e) { - remove(bar); JTextArea t = new JTextArea(e.getMessage()); t.setEditable(false); add(t); @@ -359,7 +352,11 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener { public MotorEditor(Motor m, Collection fuels) { - super(JTabbedPane.BOTTOM); + + setLayout( new BorderLayout()); + tabs = new JTabbedPane(JTabbedPane.BOTTOM); + add(tabs, BorderLayout.CENTER); + for ( Fuel f : fuels ) addFuel(f); setMotor(m); @@ -377,11 +374,11 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener { motor.addPropertyChangeListener(this); if (grainEditor != null) remove(grainEditor); - while (getTabCount() > 1) - removeTabAt(1); - add(new CaseEditor(motor.getNozzle(), motor.getChamber()), CASING_TAB); - add(new GrainEditor(motor.getGrain()), GRAIN_TAB); - add(bt = new BurnTab(), BURN_TAB); + while (tabs.getTabCount() > 1) + tabs.removeTabAt(1); + tabs.add(new CaseEditor(motor.getNozzle(), motor.getChamber()), CASING_TAB); + tabs.add(new GrainEditor(motor.getGrain()), GRAIN_TAB); + tabs.add(bt = new BurnTab(), BURN_TAB); } public static Motor defaultMotor() { @@ -421,9 +418,9 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener { public void focusOnObject(Object o) { if (o instanceof Grain) - setSelectedIndex(GRAIN_TAB); + tabs.setSelectedIndex(GRAIN_TAB); if (o instanceof Chamber || o instanceof Nozzle) - setSelectedIndex(CASING_TAB); + tabs.setSelectedIndex(CASING_TAB); } public void addBurnWatcher(BurnWatcher bw) {