use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.
the class XmlCircuitReader method getComponent.
/**
* Get a circuit's component from a read XML file. If the component has a
* non-null "trackercomp" field, it means that it is tracked, therefore it
* is skipped in the non-tracked version to avoid errors.
*
* @param elt
* XML element to parse
* @param reader
* XML file reader
* @return the component built from its XML description
* @throws XmlReaderException
*/
static Component getComponent(Element elt, XmlReader.ReadContext reader) throws XmlReaderException {
if (elt.getAttribute("trackercomp") != "" && !Main.VERSION.hasTracker()) {
return (null);
}
// Determine the factory that creates this element
String name = elt.getAttribute("name");
if (name == null || name.equals("")) {
throw new XmlReaderException(Strings.get("compNameMissingError"));
}
String libName = elt.getAttribute("lib");
Library lib = reader.findLibrary(libName);
if (lib == null) {
throw new XmlReaderException(Strings.get("compUnknownError", "no-lib"));
}
Tool tool = lib.getTool(name);
if (tool == null || !(tool instanceof AddTool)) {
if (libName == null || libName.equals("")) {
throw new XmlReaderException(Strings.get("compUnknownError", name));
} else {
throw new XmlReaderException(Strings.get("compAbsentError", name, libName));
}
}
ComponentFactory source = ((AddTool) tool).getFactory();
// Determine attributes
String loc_str = elt.getAttribute("loc");
AttributeSet attrs = source.createAttributeSet();
reader.initAttributeSet(elt, attrs, source);
// Create component if location known
if (loc_str == null || loc_str.equals("")) {
throw new XmlReaderException(Strings.get("compLocMissingError", source.getName()));
} else {
try {
Location loc = Location.parse(loc_str);
return source.createComponent(loc, attrs);
} catch (NumberFormatException e) {
throw new XmlReaderException(Strings.get("compLocInvalidError", source.getName(), loc_str));
}
}
}
use of com.cburch.logisim.comp.ComponentFactory 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.comp.ComponentFactory in project logisim-evolution by reds-heig.
the class AddTool method draw.
@Override
public void draw(Canvas canvas, ComponentDrawContext context) {
// next "if" suggested roughly by Kevin Walsh of Cornell to take care of
// repaint problems on OpenJDK under Ubuntu
int x = lastX;
int y = lastY;
if (x == INVALID_COORD || y == INVALID_COORD)
return;
ComponentFactory source = getFactory();
if (source == null)
return;
/* take care of coloring the components differently that require a label */
if (state == SHOW_GHOST) {
source.drawGhost(context, Color.GRAY, x, y, getBaseAttributes());
} else if (state == SHOW_ADD) {
source.drawGhost(context, Color.BLACK, x, y, getBaseAttributes());
}
}
use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.
the class AddTool method mouseReleased.
@Override
public void mouseReleased(Canvas canvas, Graphics g, MouseEvent e) {
Component added = null;
if (state == SHOW_ADD) {
Circuit circ = canvas.getCircuit();
if (!canvas.getProject().getLogisimFile().contains(circ))
return;
if (shouldSnap)
Canvas.snapToGrid(e);
moveTo(canvas, g, e.getX(), e.getY());
Location loc = Location.create(e.getX(), e.getY());
ComponentFactory source = getFactory();
AttributeSet attrsCopy = (AttributeSet) attrs.clone();
if (attrsCopy.containsAttribute(StdAttr.LABEL)) {
attrsCopy.setValue(StdAttr.LABEL, AutoLabler.GetCurrent(canvas.getCircuit(), source));
if (AutoLabler.IsActive(canvas.getCircuit())) {
if (AutoLabler.hasNext(canvas.getCircuit()))
AutoLabler.GetNext(canvas.getCircuit(), source);
else
AutoLabler.Stop(canvas.getCircuit());
} else
AutoLabler.SetLabel("", canvas.getCircuit(), source);
}
if (source == null)
return;
Component c = source.createComponent(loc, attrsCopy);
if (circ.hasConflict(c)) {
canvas.setErrorMessage(Strings.getter("exclusiveError"));
return;
}
Bounds bds = c.getBounds(g);
if (bds.getX() < 0 || bds.getY() < 0) {
canvas.setErrorMessage(Strings.getter("negativeCoordError"));
return;
}
try {
CircuitMutation mutation = new CircuitMutation(circ);
mutation.add(c);
Action action = mutation.toAction(Strings.getter("addComponentAction", factory.getDisplayGetter()));
canvas.getProject().doAction(action);
lastAddition = action;
added = c;
canvas.repaint();
} catch (CircuitException ex) {
JOptionPane.showMessageDialog(canvas.getProject().getFrame(), ex.getMessage());
}
setState(canvas, SHOW_GHOST);
} else if (state == SHOW_ADD_NO) {
setState(canvas, SHOW_NONE);
}
Project proj = canvas.getProject();
Tool next = determineNext(proj);
if (next != null) {
proj.setTool(next);
Action act = SelectionActions.dropAll(canvas.getSelection());
if (act != null) {
proj.doAction(act);
}
if (added != null)
canvas.getSelection().add(added);
}
}
use of com.cburch.logisim.comp.ComponentFactory in project logisim-evolution by reds-heig.
the class AddTool method getFacing.
private Direction getFacing() {
ComponentFactory source = getFactory();
if (source == null)
return Direction.NORTH;
AttributeSet base = getBaseAttributes();
Object feature = source.getFeature(ComponentFactory.FACING_ATTRIBUTE_KEY, base);
@SuppressWarnings("unchecked") Attribute<Direction> attr = (Attribute<Direction>) feature;
if (attr != null)
return base.getValue(attr);
else
return Direction.NORTH;
}
Aggregations