use of com.cburch.logisim.gui.main.Selection in project logisim-evolution by reds-heig.
the class MenuTool method mousePressed.
@Override
public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) {
int x = e.getX();
int y = e.getY();
Location pt = Location.create(x, y);
JPopupMenu menu;
Project proj = canvas.getProject();
Selection sel = proj.getSelection();
Collection<Component> in_sel = sel.getComponentsContaining(pt, g);
if (!in_sel.isEmpty()) {
Component comp = in_sel.iterator().next();
if (sel.getComponents().size() > 1) {
menu = new MenuSelection(proj);
} else {
menu = new MenuComponent(proj, canvas.getCircuit(), comp);
MenuExtender extender = (MenuExtender) comp.getFeature(MenuExtender.class);
if (extender != null)
extender.configureMenu(menu, proj);
}
} else {
Collection<Component> cl = canvas.getCircuit().getAllContaining(pt, g);
if (!cl.isEmpty()) {
Component comp = cl.iterator().next();
menu = new MenuComponent(proj, canvas.getCircuit(), comp);
MenuExtender extender = (MenuExtender) comp.getFeature(MenuExtender.class);
if (extender != null)
extender.configureMenu(menu, proj);
} else {
menu = null;
}
}
if (menu != null) {
canvas.showPopupMenu(menu, x, y);
}
}
use of com.cburch.logisim.gui.main.Selection in project logisim-evolution by reds-heig.
the class SelectTool method mousePressed.
@Override
public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) {
Project proj = canvas.getProject();
Selection sel = proj.getSelection();
Circuit circuit = canvas.getCircuit();
start = Location.create(e.getX(), e.getY());
curDx = 0;
curDy = 0;
moveGesture = null;
// if the user clicks into the selection,
// selection is being modified
Collection<Component> in_sel = sel.getComponentsContaining(start, g);
if (!in_sel.isEmpty()) {
if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
setState(proj, MOVING);
proj.repaintCanvas();
return;
} else {
Action act = SelectionActions.drop(sel, in_sel);
if (act != null) {
proj.doAction(act);
}
}
}
// if the user clicks into a component outside selection, user
// wants to add/reset selection
Collection<Component> clicked = circuit.getAllContaining(start, g);
if (!clicked.isEmpty()) {
if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
if (sel.getComponentsContaining(start).isEmpty()) {
Action act = SelectionActions.dropAll(sel);
if (act != null) {
proj.doAction(act);
}
}
}
for (Component comp : clicked) {
if (!in_sel.contains(comp)) {
sel.add(comp);
}
}
setState(proj, MOVING);
proj.repaintCanvas();
return;
}
// selection (maybe with the shift key down).
if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
Action act = SelectionActions.dropAll(sel);
if (act != null) {
proj.doAction(act);
}
}
setState(proj, RECT_SELECT);
proj.repaintCanvas();
}
use of com.cburch.logisim.gui.main.Selection in project logisim-evolution by reds-heig.
the class SelectTool method processKeyEvent.
private void processKeyEvent(Canvas canvas, KeyEvent e, int type) {
HashMap<Component, KeyConfigurator> handlers = keyHandlers;
if (handlers == null) {
handlers = new HashMap<Component, KeyConfigurator>();
Selection sel = canvas.getSelection();
for (Component comp : sel.getComponents()) {
ComponentFactory factory = comp.getFactory();
AttributeSet attrs = comp.getAttributeSet();
Object handler = factory.getFeature(KeyConfigurator.class, attrs);
if (handler != null) {
KeyConfigurator base = (KeyConfigurator) handler;
handlers.put(comp, base.clone());
}
}
keyHandlers = handlers;
}
if (!handlers.isEmpty()) {
boolean consume = false;
ArrayList<KeyConfigurationResult> results;
results = new ArrayList<KeyConfigurationResult>();
for (Map.Entry<Component, KeyConfigurator> entry : handlers.entrySet()) {
Component comp = entry.getKey();
KeyConfigurator handler = entry.getValue();
KeyConfigurationEvent event = new KeyConfigurationEvent(type, comp.getAttributeSet(), e, comp);
KeyConfigurationResult result = handler.keyEventReceived(event);
consume |= event.isConsumed();
if (result != null) {
results.add(result);
}
}
if (consume) {
e.consume();
}
if (!results.isEmpty()) {
SetAttributeAction act = new SetAttributeAction(canvas.getCircuit(), Strings.getter("changeComponentAttributesAction"));
for (KeyConfigurationResult result : results) {
Component comp = (Component) result.getEvent().getData();
Map<Attribute<?>, Object> newValues = result.getAttributeValues();
for (Map.Entry<Attribute<?>, Object> entry : newValues.entrySet()) {
act.set(comp, entry.getKey(), entry.getValue());
}
}
if (!act.isEmpty()) {
canvas.getProject().doAction(act);
}
}
}
}
use of com.cburch.logisim.gui.main.Selection in project logisim-evolution by reds-heig.
the class SelectTool method mouseReleased.
@Override
public void mouseReleased(Canvas canvas, Graphics g, MouseEvent e) {
Project proj = canvas.getProject();
if (state == MOVING) {
setState(proj, IDLE);
computeDxDy(proj, e, g);
int dx = curDx;
int dy = curDy;
if (dx != 0 || dy != 0) {
if (!proj.getLogisimFile().contains(canvas.getCircuit())) {
canvas.setErrorMessage(Strings.getter("cannotModifyError"));
} else if (proj.getSelection().hasConflictWhenMoved(dx, dy)) {
canvas.setErrorMessage(Strings.getter("exclusiveError"));
} else {
boolean connect = shouldConnect(canvas, e.getModifiersEx());
drawConnections = false;
ReplacementMap repl;
if (connect) {
MoveGesture gesture = moveGesture;
if (gesture == null) {
gesture = new MoveGesture(new MoveRequestHandler(canvas), canvas.getCircuit(), canvas.getSelection().getAnchoredComponents());
}
canvas.setErrorMessage(new ComputingMessage(dx, dy), COLOR_COMPUTING);
MoveResult result = gesture.forceRequest(dx, dy);
clearCanvasMessage(canvas, dx, dy);
repl = result.getReplacementMap();
} else {
repl = null;
}
Selection sel = proj.getSelection();
proj.doAction(SelectionActions.translate(sel, dx, dy, repl));
}
}
moveGesture = null;
proj.repaintCanvas();
} else if (state == RECT_SELECT) {
Bounds bds = Bounds.create(start).add(start.getX() + curDx, start.getY() + curDy);
Circuit circuit = canvas.getCircuit();
Selection sel = proj.getSelection();
Collection<Component> in_sel = sel.getComponentsWithin(bds, g);
for (Component comp : circuit.getAllWithin(bds, g)) {
if (!in_sel.contains(comp))
sel.add(comp);
}
Action act = SelectionActions.drop(sel, in_sel);
if (act != null) {
proj.doAction(act);
}
setState(proj, IDLE);
proj.repaintCanvas();
}
if (e.getClickCount() >= 2) {
Set<Component> comps = canvas.getProject().getSelection().getComponents();
if (comps.size() == 1) {
for (Component comp : comps) {
if (comp.getAttributeSet().containsAttribute(StdAttr.LABEL)) {
String OldLabel = comp.getAttributeSet().getValue(StdAttr.LABEL);
SetAttributeAction act = new SetAttributeAction(canvas.getCircuit(), Strings.getter("changeComponentAttributesAction"));
AutoLabler.AskAndSetLabel(comp.getFactory().getDisplayName(), OldLabel, canvas.getCircuit(), comp, comp.getFactory(), comp.getAttributeSet(), act, true);
if (!act.isEmpty())
canvas.getProject().doAction(act);
}
}
}
}
}
Aggregations