public void letterOrDigitTyped() { table.setAllowEditing(true); table.repaint(); // G.Sturr 2009-10-10: cleanup when keypress edit begins // check if cell fixed Object o = model.getValueAt(table.getSelectedRow(), table.getSelectedColumn()); if (o != null && o instanceof GeoElement) { GeoElement geo = (GeoElement) o; if (geo.isFixed()) return; } model.setValueAt(null, table.getSelectedRow(), table.getSelectedColumn()); table.editCellAt(table.getSelectedRow(), table.getSelectedColumn()); // workaround, see // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4192625 final JTextComponent f = (JTextComponent) table.getEditorComponent(); f.requestFocus(); f.getCaret().setVisible(true); // workaround for Mac OS X 10.5 problem (first character typed deleted) if (Application.MAC_OS) SwingUtilities.invokeLater( new Runnable() { public void run() { f.setSelectionStart(1); f.setSelectionEnd(1); } }); table.setAllowEditing(false); }
public final void perform(Command c) throws MyError { int n = c.getArgumentNumber(); GeoElement[] arg; switch (n) { case 1: arg = resArgs(c); if (arg[0].isNumberValue()) { GeoNumeric layerGeo = (GeoNumeric) arg[0]; int layer = (int) layerGeo.getDouble(); Iterator<GeoElement> it = kernelA.getConstruction().getGeoSetLabelOrder().iterator(); while (it.hasNext()) { GeoElement geo = it.next(); if (geo.getLayer() == layer) { geo.setEuclidianVisible(true); geo.updateRepaint(); } } return; } else throw argErr(app, c.getName(), null); default: throw argNumErr(app, c.getName(), n); } }
public final void compute() { // Check if the points are aligned double x1 = -A.inhomX; double y1 = -A.inhomY; double x2 = -B.inhomX; double y2 = -B.inhomY; double x3 = -C.inhomX; double y3 = -C.inhomY; double det = (-x2 + x3) * (y1 - y3) + (x1 - x3) * (y2 - y3); if (Kernel.isZero(det)) { poly.setUndefined(); } else { ycoef[0].setValue((x3 - x2) / det); xcoef[0].setValue((y2 - y3) / det); constant[0].setValue(((x3 - x2) * y3 + (y2 - y3) * x3) / det); ycoef[1].setValue((x1 - x3) / det); xcoef[1].setValue((y3 - y1) / det); constant[1].setValue(((x1 - x3) * y1 + (y3 - y1) * x1) / det); ycoef[2].setValue((x2 - x1) / det); xcoef[2].setValue((y1 - y2) / det); constant[2].setValue(((x2 - x1) * y2 + (y1 - y2) * x2) / det); dd.update(); poly.update(); } }
/** * Performs a vertical spreadsheet drag-copy. Cells are copied vertically row by row using a * single given row as the copy source. * * @param x1 minimum column of the drag-copy region * @param x2 maximum column of the drag-copy region * @param sy source row * @param dy1 destination minimum row * @param dy2 destination maximum row * @throws Exception */ public void doCopyVerticalNoStoringUndoInfo1(int x1, int x2, int sy, int dy1, int dy2) throws Exception { // create a treeset, ordered by construction index // so that when we relative copy A1=1 B1=(A1+C1)/2 C1=3 // B2 is done last TreeSet<GeoElement> tree = new TreeSet<GeoElement>(); for (int x = x1; x <= x2; ++x) { int ix = x - x1; GeoElement cell = getValue(app, x1 + ix, sy); if (cell != null) { tree.add(cell); } } for (int y = dy1; y <= dy2; ++y) { int iy = y - dy1; Iterator<GeoElement> iterator = tree.iterator(); while (iterator.hasNext()) { GeoElement geo = (iterator.next()); if (geo != null) { GPoint p = geo.getSpreadsheetCoords(); doCopyNoStoringUndoInfo0(kernel, app, geo, getValue(app, p.x, dy1 + iy), 0, y - sy); // Application.debug(p.x+""); } } } }
@Override public final GeoElement[] process(Command c) throws MyError { int n = c.getArgumentNumber(); // avoid // "Command Sequence not known eg Sequence[If[Element[list1,i]=="b",0,1]] if (n < 3 || n % 2 == 0) throw argNumErr(app, c.getName(), n); // create local variable at position 1 and resolve arguments GeoElement arg = null; GeoElement[] vars = new GeoElement[n / 2]; GeoList[] over = new GeoList[n / 2]; boolean oldval = cons.isSuppressLabelsActive(); try { cons.setSuppressLabelCreation(true); arg = resArgsForZip(c, vars, over); } finally { for (GeoElement localVar : vars) { if (localVar != null) cons.removeLocalVariable(localVar.getLabel(StringTemplate.defaultTemplate)); } cons.setSuppressLabelCreation(oldval); } AlgoZip algo = new AlgoZip(cons, c.getLabel(), arg, vars, over); return algo.getOutput(); }
/** * Performs a horizontal spreadsheet drag-copy. Cells are copied horizontally column by column * using a single given column as the copy source. * * @param y1 minimum row of the drag-copy region * @param y2 maximum row of the drag-copy region * @param sx source column * @param dx1 destination minimum column * @param dx2 destination maximum column * @throws Exception */ public void doCopyHorizontalNoStoringUndoInfo1(int y1, int y2, int sx, int dx1, int dx2) throws Exception { // create a treeset, ordered by construction index // so that when we relative copy A1=1 A2=(A1+A3)/2 A3=3 // B2 is done last TreeSet<GeoElement> tree = new TreeSet<GeoElement>(); for (int y = y1; y <= y2; ++y) { int iy = y - y1; GeoElement cell = getValue(app, sx, y1 + iy); if (cell != null) { tree.add(cell); } } for (int x = dx1; x <= dx2; ++x) { int ix = x - dx1; Iterator<GeoElement> iterator = tree.iterator(); while (iterator.hasNext()) { GeoElement geo = (iterator.next()); if (geo != null) { GPoint p = geo.getSpreadsheetCoords(); doCopyNoStoringUndoInfo0(kernel, app, geo, getValue(app, dx1 + ix, p.y), x - sx, 0); // Application.debug(p.y+""); } } } }
public final void perform(Command c) throws MyError { int n = c.getArgumentNumber(); GeoElement[] arg; switch (n) { case 2: arg = resArgs(c); if (arg[1].isGeoText()) { GeoElement geo = (GeoElement) arg[0]; if (RenameInputHandler.checkName(geo, ((GeoText) arg[1]).getTextString())) { geo.rename(((GeoText) arg[1]).getTextString()); geo.updateRepaint(); return; } else { throw argErr(app, c.getName(), arg[1]); } } else throw argErr(app, c.getName(), arg[1]); default: throw argNumErr(app, c.getName(), n); } }
/** * Returns array of GeoElements that depend on given GeoElement geo * * @param geo * @return */ public static GeoElement[] getDependentObjects(GeoElement geo) { if (geo.isIndependent()) { return new GeoElement[0]; } TreeSet<GeoElement> geoTree = geo.getAllPredecessors(); return geoTree.toArray(new GeoElement[0]); }
public final String toAssignment(GeoElement ge) { String body = ge.getCASString(false); String casLabel = ge.getLabel(); if (ge instanceof FunctionalNVar) { String params = ((FunctionalNVar) ge).getFunction().getVarString(); return translateFunctionDeclaration(casLabel, params, body); } return translateAssignment(casLabel, body); }
private void applyUseAsText(ArrayList<GeoElement> geos) { // btnUseAsText for (int i = 0; i < geos.size(); i++) { GeoElement geo = geos.get(i); if (geo instanceof GeoCasCell) { ((GeoCasCell) geo).setUseAsText(btnUseAsText.isSelected()); geo.updateRepaint(); needUndo = true; } } }
private void doActionPerformed() { NumberValue newVal = kernel.getAlgebraProcessor().evaluateToNumeric(tfAnimStep.getText(), true); if (newVal != null && !Double.isNaN(newVal.getDouble())) { for (int i = 0; i < geos.length; i++) { GeoElement geo = (GeoElement) geos[i]; geo.setAnimationStep(newVal); geo.updateRepaint(); } } update(geos); }
private void buildLocusMacroConstruction(TreeSet<ConstructionElement> locusConsElements) { // build macro construction macroKernel = new MacroKernel((Kernel) kernel); macroKernel.setGlobalVariableLookup(true); // tell the macro construction about reserved names: // these names will not be looked up in the parent // construction Iterator<ConstructionElement> it = locusConsElements.iterator(); while (it.hasNext()) { ConstructionElement ce = it.next(); if (ce.isGeoElement()) { GeoElement geo = (GeoElement) ce; macroKernel.addReservedLabel(geo.getLabel()); } } try { // get XML for macro construction of P -> Q String locusConsXML = Macro.buildMacroXML((Kernel) kernel, locusConsElements); macroKernel.loadXML(locusConsXML); // get the copies of P and Q from the macro kernel Pcopy = (GeoPoint2) macroKernel.lookupLabel(movingPoint.label); Pcopy.setFixed(false); Pcopy.setPath(movingPoint.getPath()); Qcopy = (GeoPoint2) macroKernel.lookupLabel(locusPoint.label); macroCons = macroKernel.getConstruction(); /* // make sure that the references to e.g. start/end point of a segment are not // changed later on. This is achieved by setting isMacroOutput to true it = macroCons.getGeoElementsIterator(); while (it.hasNext()) { GeoElement geo = (GeoElement) it.next(); geo.isAlgoMacroOutput = true; } Pcopy.isAlgoMacroOutput = false; */ } catch (Exception e) { e.printStackTrace(); locus.setUndefined(); macroCons = null; } // //Application.debug("P: " + P + ", kernel class: " + P.kernel.getClass()); // Application.debug("Pcopy: " + Pcopy + ", kernel class: " + Pcopy.kernel.getClass()); // //Application.debug("P == Pcopy: " + (P == Pcopy)); // //Application.debug("Q: " + Q + ", kernel class: " + Q.kernel.getClass()); // Application.debug("Qcopy: " + Qcopy + ", kernel class: " + Qcopy.kernel.getClass()); // //Application.debug("Q == Qcopy: " + (Q == Qcopy)); }
/* * Michael Borcherds 2008-03-03 return -1 if nothing selected return -2 if * objects from more than one layer selected return layer number if objects * from exactly one layer are selected */ private int getSelectedLayer() { Object[] geos = app.getSelectedGeos().toArray(); if (geos.length == 0) return -1; // return -1 if nothing selected int layer = ((GeoElement) geos[0]).getLayer(); for (int i = 1; i < geos.length; i++) { GeoElement geo = (GeoElement) geos[i]; if (geo.getLayer() != layer) return -2; // return -2 if more than one layer selected } return layer; }
private void applyTextColor(ArrayList<GeoElement> geos) { Color color = geogebra.awt.GColorD.getAwtColor(btnTextColor.getSelectedColor()); for (int i = 0; i < geos.size(); i++) { GeoElement geo = geos.get(i); if (geo instanceof GeoCasCell) { ((GeoCasCell) geo).setFontColor(new geogebra.awt.GColorD(color)); geo.updateRepaint(); needUndo = true; } } }
/** * Tests if a cell range can be used as the source for a pattern drag-copy. * * @param cellRange * @return */ private static boolean isPatternSource(CellRange cellRange) { // don't allow empty cells if (cellRange.hasEmptyCells()) { return false; } // test for any unacceptable geos in the range ArrayList<GeoElement> list = cellRange.toGeoList(); for (GeoElement geo : list) { if (!(geo.isGeoNumeric() || geo.isGeoFunction() || geo.isGeoPoint())) { return false; } } return true; }
private void applyFontStyle(ArrayList<GeoElement> geos) { int fontStyle = 0; if (btnBold.isSelected()) fontStyle += 1; if (btnItalic.isSelected()) fontStyle += 2; App.printStacktrace(geos.size() + ""); for (int i = 0; i < geos.size(); i++) { GeoElement geo = geos.get(i); App.debug(((GeoCasCell) geo).getGeoText()); if (geo instanceof GeoCasCell && ((GeoCasCell) geo).getGeoText().getFontStyle() != fontStyle) { ((GeoCasCell) geo).getGeoText().setFontStyle(fontStyle); geo.updateRepaint(); needUndo = true; } } }
private void applyTextSize(ArrayList<GeoElement> geos) { double fontSize = GeoText.getRelativeFontSize( btnTextSize.getSelectedIndex()); // transform indices to the range -4, .. , // 4 for (int i = 0; i < geos.size(); i++) { GeoElement geo = geos.get(i); if (geo instanceof GeoCasCell && ((GeoCasCell) geo).getGeoText().getFontSizeMultiplier() != fontSize) { ((GeoCasCell) geo).setFontSizeMultiplier(fontSize); geo.updateRepaint(); needUndo = true; } } }
// calc the current value of the arithmetic tree @Override public final void compute() { String col = GeoElementSpreadsheet.getSpreadsheetColumnName( geo.getLabel(StringTemplate.defaultTemplate)); if (col == null) text.setUndefined(); else text.setTextString(col); }
/** * Prepares a spreadsheet cell editor string for processing in the kernel and returns either (1) a * new GeoElement for the cell or (2) null. * * @param kernel kernel * @param app application * @param inputText string representation of the new GeoElement * @param oldValue current cell GeoElement * @param column cell column * @param row cell row * @return either (1) a new GeoElement for the cell or (2) null * @throws Exception */ public static GeoElement prepareAddingValueToTableNoStoringUndoInfo( Kernel kernel, App app, String inputText, GeoElement oldValue, int column, int row) throws Exception { String text = inputText; // get the cell name String name = GeoElementSpreadsheet.getSpreadsheetCellName(column, row); // trim the text if (text != null) { text = text.trim(); if (text.length() == 0) { text = null; } } // if "=" is required before commands and text is not a number // or does not begin with "=" then surround it with quotes. // This will force the cell to become GeoText. if (app.getSettings().getSpreadsheet().equalsRequired() && text != null) { if (!((text.charAt(0) == '=') || isNumber(text))) { text = "\"" + text + "\""; } } // if the cell is currently GeoText then prepare it for changes // make sure it can be changed to something else // eg (2,3 can be overwritten as (2,3) // if (oldValue != null && oldValue.isGeoText() && // !oldValue.hasChildren()) { // oldValue.remove(); // oldValue = null; // } // if the text is null then remove the current cell geo and return null if (text == null) { if (oldValue != null) { oldValue.remove(); } return null; // else if the target cell is empty, try to create a new GeoElement // for this cell } else if (oldValue == null) { try { // this will be a new geo return prepareNewValue(kernel, name, text); } catch (Throwable t) { return prepareNewValue(kernel, name, ""); } // else the target cell is an existing GeoElement, so redefine it } else { return updateOldValue(kernel, oldValue, name, text); } }
// calc the current value of the arithmetic tree @Override public final void compute() { boolean useLaTeX = true; if (!geo.isDefined() || (substituteVars != null && !substituteVars.isDefined()) || showName != null && !showName.isDefined()) { text.setTextString(""); } else { boolean substitute = substituteVars == null ? true : substituteVars.getBoolean(); boolean show = showName == null ? false : showName.getBoolean(); text.setTemporaryPrintAccuracy(); // Application.debug(geo.getFormulaString(StringType.LATEX, substitute )); if (show) { text.setTextString(geo.getLaTeXAlgebraDescription(substitute)); if (text.getTextString() == null) { text.setTextString(geo.getAlgebraDescriptionTextOrHTML()); useLaTeX = false; } } else { if (geo.isGeoText()) { // needed for eg Text commands eg FormulaText[Text[ text.setTextString(((GeoText) geo).getTextString()); } else { text.setTextString(geo.getFormulaString(StringType.LATEX, substitute)); } } text.restorePrintAccuracy(); } text.setLaTeX(useLaTeX, false); /* int tempCASPrintForm = kernel.getCASPrintForm(); kernel.setCASPrintForm(StringType.LATEX); text.setTextString(geo.getCommandDescription()); kernel.setCASPrintForm(tempCASPrintForm);*/ }
@SuppressWarnings("unused") private void closeDialog() { // either remove our geo or keep it and make it visible if (keepNewGeo) { addNewGeoToConstruction(); } else { newGeo.remove(); } setVisible(false); }
private void init() { // copy the construction Qin = locusPoint.getAllPredecessors(); // all parents of Q // get intersection of all children of P and all parents of Q locusConsOrigElements = new TreeSet<ConstructionElement>(); TreeSet<Long> usedAlgoIds = new TreeSet<Long>(); Iterator<GeoElement> it = Qin.iterator(); while (it.hasNext()) { GeoElement parent = it.next(); if (parent.isLabelSet() && parent.isChildOf(movingPoint)) { // note: locusConsOrigElements will contain AlgoElement and GeoElement objects Macro.addDependentElement(parent, locusConsOrigElements, usedAlgoIds); } } // ensure that P and Q have labels set // Note: we have to undo this at the end of this method !!! boolean isLabeledP = movingPoint.isLabelSet(); if (!isLabeledP) { movingPoint.label = movingPoint.getDefaultLabel(); movingPoint.labelSet = true; } boolean isLabeledQ = locusPoint.isLabelSet(); if (!isLabeledQ) { locusPoint.label = locusPoint.getDefaultLabel(); locusPoint.labelSet = true; } // add moving point on line locusConsOrigElements.add(movingPoint); // add locus creating point and its algorithm to locusConsOrigElements Macro.addDependentElement(locusPoint, locusConsOrigElements, usedAlgoIds); // create macro construction buildLocusMacroConstruction(locusConsOrigElements); // if we used temp labels remove them again if (!isLabeledP) movingPoint.labelSet = false; if (!isLabeledQ) locusPoint.labelSet = false; }
/** * @param kernel kernel * @param app application * @param text definition text * @param geoForStyle geo to be used for style of output * @param column column * @param row row * @throws Exception if definition of new geo fails */ public static void doCopyNoStoringUndoInfo1( Kernel kernel, App app, String text, GeoElement geoForStyle, int column, int row) throws Exception { GeoElement oldValue = getValue(app, column, row); if (text == null) { if (oldValue != null) { prepareAddingValueToTableNoStoringUndoInfo(kernel, app, null, oldValue, column, row); } return; } GeoElement value2 = prepareAddingValueToTableNoStoringUndoInfo(kernel, app, text, oldValue, column, row); if (geoForStyle != null) { value2.setVisualStyle(geoForStyle); } }
@Override public final void compute() { size = inputList.size(); if (!inputList.isDefined() || !function.toGeoElement().isDefined()) { r2.setUndefined(); return; } GeoFunction funGeo = function.getGeoFunction(); // Calculate errorsum and ssy: double sumyy = 0.0d; double sumy = 0.0d; double syy = 0.0d; double errorsum = 0.0d; GeoElement geo = null; GeoPoint point = null; double x, y, v; for (int i = 0; i < size; i++) { geo = inputList.get(i); if (geo.isGeoPoint()) { point = (GeoPoint) geo; x = point.getX(); y = point.getY(); v = funGeo.evaluate(x); errorsum += (v - y) * (v - y); sumy += y; sumyy += y * y; } else { r2.setUndefined(); return; } // if calculation is possible } // for all points syy = sumyy - sumy * sumy / size; // calculate RSquare r2.setValue(1 - errorsum / syy); } // compute()
/** * Resolves arguments, creates local variables and fills the vars and overlists * * @param c * @return list of arguments */ protected final GeoElement[] resArgsForZip(Command c) { // check if there is a local variable in arguments int numArgs = c.getArgumentNumber(); vars = new GeoElement[numArgs / 2]; over = new GeoList[numArgs / 2]; Construction cmdCons = (Construction) c.getKernel().getConstruction(); for (int varPos = 1; varPos < numArgs; varPos += 2) { String localVarName = c.getVariableName(varPos); if (localVarName == null) { throw argErr(app, c.getName(), c.getArgument(varPos)); } // add local variable name to construction GeoElement num = null; // initialize first value of local numeric variable from initPos boolean oldval = cons.isSuppressLabelsActive(); cons.setSuppressLabelCreation(true); GeoList gl = (GeoList) resArg(c.getArgument(varPos + 1))[0]; cons.setSuppressLabelCreation(oldval); num = gl.get(0).copyInternal(cons); cmdCons.addLocalVariable(localVarName, num); // set local variable as our varPos argument c.setArgument(varPos, new ExpressionNode(c.getKernel(), num)); vars[varPos / 2] = num.toGeoElement(); over[varPos / 2] = gl; // resolve all command arguments including the local variable just // created // remove local variable name from kernel again } GeoElement[] arg = resArgs(c); for (GeoElement localVar : vars) cmdCons.removeLocalVariable(localVar.getLabel()); return arg; }
// for AlgoElement @Override protected void setInputOutput() { // it is inefficient to have Q and P as input // let's take all independent parents of Q // and the path as input TreeSet<GeoElement> inSet = new TreeSet<GeoElement>(); inSet.add((GeoElement) path.toGeoElement()); // we need all independent parents of Q PLUS // all parents of Q that are points on a path Iterator<GeoElement> it = Qin.iterator(); while (it.hasNext()) { GeoElement geo = it.next(); if (geo.isIndependent() || geo.isPointOnPath()) { inSet.add(geo); } } // remove P from input set! inSet.remove(movingPoint); efficientInput = new GeoElement[inSet.size()]; it = inSet.iterator(); int i = 0; while (it.hasNext()) { efficientInput[i] = (GeoElement) it.next(); i++; } // the standardInput array should be used for // the dependency graph standardInput = new GeoElement[2]; standardInput[0] = locusPoint; standardInput[1] = movingPoint; setOutputLength(1); setOutput(0, locus); // handle dependencies setEfficientDependencies(standardInput, efficientInput); }
private void addNewGeoToConstruction() { if (objectType == TYPE_LISTOFPOINTS || objectType == TYPE_POLYLINE) { app.getKernel().getConstruction().addToConstructionList(newGeo.getParentAlgorithm(), true); } newGeo.setEuclidianVisible(true); if (!newGeo.isGeoText()) newGeo.setAuxiliaryObject(false); if (objectType == TYPE_LISTOFPOINTS) { GeoList gl = (GeoList) newGeo; for (int i = 0; i < gl.size(); i++) { gl.get(i).setEuclidianVisible(true); gl.get(i).setAuxiliaryObject(false); } } if (objectType == TYPE_POLYLINE) { GeoPoint[] pts = ((AlgoPolyLine) newGeo.getParentAlgorithm()).getPoints(); for (int i = 0; i < pts.length; i++) { pts[i].setEuclidianVisible(true); pts[i].setAuxiliaryObject(false); } } newGeo.update(); app.storeUndoInfo(); }
@Override protected final void perform(Command c) throws MyError { int n = c.getArgumentNumber(); switch (n) { case 2: arg = resArgs(c); if (arg[1].isGeoNumeric()) { GeoElement geo = arg[0]; geo.setTooltipMode((int) ((GeoNumeric) arg[1]).getDouble()); geo.updateRepaint(); return; } throw argErr(app, c.getName(), arg[1]); default: throw argNumErr(app, c.getName(), n); } }
/** Set all elements in locusConsElements to the current values of the main construction */ private void resetMacroConstruction() { Iterator<ConstructionElement> it = locusConsOrigElements.iterator(); while (it.hasNext()) { ConstructionElement ce = it.next(); if (ce.isGeoElement()) { GeoElement geoOrig = (GeoElement) ce; // do not copy functions, their expressions already // include references to the correct other geos if (!geoOrig.isGeoFunction()) { GeoElement geoCopy = macroCons.lookupLabel(geoOrig.label); if (geoCopy != null) { try { geoCopy.set(geoOrig); geoCopy.update(); } catch (Exception e) { Application.debug("AlgoLocus: error in resetMacroConstruction(): " + e.getMessage()); } } } } } }
private boolean checkGeos(Object[] geos) { boolean geosOK = true; for (int i = 0; i < geos.length; i++) { GeoElement geo = (GeoElement) geos[i]; if (!geo.isChangeable() || geo.isGeoText() || geo.isGeoImage() || geo.isGeoList() || geo.isGeoBoolean() || geo.isGeoButton() || (!partOfSliderPanel && geo.isGeoNumeric() && geo.isIndependent()) // slider ) { geosOK = false; break; } } return geosOK; }