better adaptive toolbar

This commit is contained in:
hneemann 2016-03-21 21:35:33 +01:00
parent e26c4f643d
commit 3f098ea1a2
4 changed files with 66 additions and 14 deletions

View File

@ -24,7 +24,8 @@ public class SpeedTest {
Clock clock = clocks.get(0); Clock clock = clocks.get(0);
model.init(true); clock.disableTimer();
model.init();
ObservableValue clockValue = clock.getOutputs()[0]; ObservableValue clockValue = clock.getOutputs()[0];
int state = (int) clockValue.getValue(); int state = (int) clockValue.getValue();

View File

@ -20,6 +20,7 @@ public class Clock implements Element {
private final ObservableValue output; private final ObservableValue output;
private final int frequency; private final int frequency;
public boolean startThisTimer = true;
public Clock(ElementAttributes attributes) { public Clock(ElementAttributes attributes) {
output = new ObservableValue("C", 1); output = new ObservableValue("C", 1);
@ -36,10 +37,13 @@ public class Clock implements Element {
return new ObservableValue[]{output}; return new ObservableValue[]{output};
} }
public void disableTimer() {
this.startThisTimer = false;
}
@Override @Override
public void registerNodes(Model model) { public void registerNodes(Model model) {
model.addObserver(new ModelStateObserver() { model.addObserver(new ModelStateObserver() {
public boolean startThisTimer = true;
public Timer timer; public Timer timer;
@Override @Override
@ -67,7 +71,7 @@ public class Clock implements Element {
break; break;
case FETCHCLOCK: case FETCHCLOCK:
event.registerClock(Clock.this); event.registerClock(Clock.this);
startThisTimer = false; break;
} }
} }
}); });

View File

@ -1,6 +1,7 @@
package de.neemann.digital.gui; package de.neemann.digital.gui;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -8,24 +9,69 @@ import java.util.ArrayList;
*/ */
public class InsertHistory { public class InsertHistory {
private static final int MAX_ICONS = 6;
private final JToolBar bar; private final JToolBar bar;
private final ArrayList<AbstractAction> actions; private final ArrayList<WrapperAction> wrappers;
private final int removePos; private int mainTime;
public InsertHistory(JToolBar bar) { public InsertHistory(JToolBar bar) {
this.bar = bar; this.bar = bar;
actions = new ArrayList<>(); wrappers = new ArrayList<>();
removePos = bar.getComponentCount();
} }
public void add(AbstractAction action) { public void add(AbstractAction action) {
if (!actions.contains(action)) { if (!contains(action)) {
actions.add(action); WrapperAction wrapper = new WrapperAction(action, bar.getComponentCount());
bar.add(action); wrappers.add(wrapper);
if (actions.size() > 3) { bar.add(wrapper);
actions.remove(0); if (wrappers.size() > MAX_ICONS) {
bar.remove(removePos); int oldest = findOldestIndex();
wrapper = wrappers.get(oldest);
bar.remove(wrapper.componentPosition);
for (int i = oldest; i < wrappers.size(); i++)
wrappers.get(i).componentPosition--;
wrappers.remove(oldest);
} }
} }
} }
private int findOldestIndex() {
int found = -1;
int oldestTime = mainTime;
for (int i = 0; i < wrappers.size(); i++) {
WrapperAction wrapper = wrappers.get(i);
if (wrapper.time < oldestTime) {
found = i;
oldestTime = wrapper.time;
}
}
return found;
}
private boolean contains(AbstractAction action) {
for (WrapperAction wrapper : wrappers)
if (wrapper.action == action)
return true;
return false;
}
public class WrapperAction extends AbstractAction {
private final AbstractAction action;
private int componentPosition;
private int time;
public WrapperAction(AbstractAction action, int componentPosition) {
super(action.getValue(Action.NAME).toString(), (Icon) action.getValue(Action.SMALL_ICON));
this.action = action;
this.componentPosition = componentPosition;
time = mainTime++;
}
@Override
public void actionPerformed(ActionEvent e) {
action.actionPerformed(e);
time = mainTime++;
}
}
} }

View File

@ -43,8 +43,9 @@ public class LEDShape implements Shape {
@Override @Override
public void drawTo(Graphic graphic) { public void drawTo(Graphic graphic) {
boolean fill = false; boolean fill = true;
if (ioState != null) { if (ioState != null) {
fill = false;
ObservableValue value = ioState.getInput(0); ObservableValue value = ioState.getInput(0);
if (!value.isHighZ() && (value.getValue() != 0)) if (!value.isHighZ() && (value.getValue() != 0))
fill = true; fill = true;