Ejemplo n.º 1
0
  // 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);
  }
Ejemplo n.º 2
0
  public AlgoColumnName(Construction cons, String label, GeoElement geo) {
    super(cons);
    this.geo = geo;

    text = new GeoText(cons);
    text.setIsTextCommand(true); // stop editing as text
    setInputOutput(); // for AlgoElement

    // compute value of dependent number
    compute();
    text.setLabel(label);
  }
Ejemplo n.º 3
0
  public void setLaTeX(Application app, String str) {

    if (str.indexOf('"') > -1) {

      GeoText text = app.getKernel().getAlgebraProcessor().evaluateToText(str, false, false);

      if (text != null) {
        text.setLaTeX(true, false);
        str = text.getTextString();
      } else {
        // bad syntax, remove all quotes and use raw string
        while (str.indexOf('"') > -1) str = str.replace('"', ' ');
        // latexPreview.setLaTeX(str);
      }
    } else {
      // latexPreview.setLaTeX(str);

    }

    String f = str.trim();
    if (f.length() >= 2 && f.startsWith("$") && f.endsWith("$")) {
      f = f.substring(1, f.length() - 1);
    }

    im =
        (BufferedImage)
            TeXFormula.getPartialTeXFormula(f)
                .createBufferedImage(
                    TeXConstants.STYLE_DISPLAY, defaultSize, Color.black, Color.white);

    /*
    icon = TeXFormula.getPartialTeXFormula(f).createTeXIcon(
    		TeXConstants.STYLE_DISPLAY, defaultSize);
    if (icon == null) {
    	icon = TeXFormula.getPartialTeXFormula("").createTeXIcon(
    			TeXConstants.STYLE_DISPLAY, defaultSize);
    }

    width = icon.getIconWidth();
    height = icon.getIconHeight();*/
    width = im.getWidth();
    height = im.getHeight();
    Dimension dim = new Dimension(width + 2 * INSET, height + 2 * INSET);
    setPreferredSize(dim);
    setSize(dim);
    setLocation(0, 0);
    setVisible(true);
    repaint();
  }
Ejemplo n.º 4
0
  public AlgoLaTeX(Construction cons, String label, GeoElement geo) {
    super(cons);
    this.geo = geo;
    this.substituteVars = null;
    this.showName = null;
    text = new GeoText(cons);

    text.setIsTextCommand(true); // stop editing as text
    setInputOutput(); // for AlgoElement

    // compute value of dependent number
    compute();
    text.setLabel(label);

    // set sans-serif LaTeX default
    text.setSerifFont(false);
  }
Ejemplo n.º 5
0
  public AlgoLaTeX(
      Construction cons,
      String label,
      GeoElement geo,
      GeoBoolean substituteVars,
      GeoBoolean showName) {
    super(cons);
    this.geo = geo;
    this.substituteVars = substituteVars;
    this.showName = showName;
    text = new GeoText(cons);
    setInputOutput(); // for AlgoElement

    // compute value of dependent number
    compute();
    text.setLabel(label);

    // set sans-serif LaTeX default
    text.setSerifFont(false);
  }
Ejemplo n.º 6
0
  // 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);*/
  }
Ejemplo n.º 7
0
  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;
      }
    }
  }
Ejemplo n.º 8
0
  private static GeoElement updateOldValue(
      Kernel kernel, GeoElement oldValue, String name, String text) throws Exception {
    String text0 = text;
    if (text.charAt(0) == '=') {
      text = text.substring(1);
    }
    GeoElement newValue = null;
    try {
      // always redefine objects in spreadsheet, don't store undo info
      // here
      newValue =
          kernel
              .getAlgebraProcessor()
              .changeGeoElementNoExceptionHandling(oldValue, text, true, false);

      // newValue.setConstructionDefaults();
      newValue.setAllVisualProperties(oldValue, true);
      if (oldValue.isAuxiliaryObject()) {
        newValue.setAuxiliaryObject(true);
      }

      // Application.debug("GeoClassType = " +
      // newValue.getGeoClassType()+" " + newValue.getGeoClassType());
      if (newValue.getGeoClassType() == oldValue.getGeoClassType()) {
        // newValue.setVisualStyle(oldValue);
      } else {
        kernel.getApplication().refreshViews();
      }
    } catch (CircularDefinitionException cde) {
      kernel.getApplication().showError("CircularDefinition");
      return null;
    } catch (Throwable e) {
      // if exception is thrown treat the input as text and try to update
      // the cell as a GeoText
      {
        // reset the text string if old value is GeoText
        if (oldValue.isGeoText()) {
          ((GeoText) oldValue).setTextString(text0);
          oldValue.updateCascade();
        }

        // if not currently a GeoText and no children, redefine the cell
        // as new GeoText
        else if (!oldValue.hasChildren()) {
          oldValue.remove();

          // add input as text
          try {
            newValue = prepareNewValue(kernel, name, "\"" + text0 + "\"");
          } catch (Throwable t) {
            newValue = prepareNewValue(kernel, name, "");
          }
          newValue.setEuclidianVisible(false);
          newValue.update();
        }

        // otherwise throw an exception and let the cell revert to the
        // old value
        else {
          throw new Exception(e);
        }
      }
    }
    return newValue;
  }
  /** Handles data import. */
  @Override
  public boolean importData(JComponent comp, Transferable t) {

    // give the drop target (this EV) the view focus
    requestViewFocus();

    Point mousePos = ev.getMousePosition();

    // ------------------------------------------
    // Import handling is done in this order:
    // 1) PlotPanel GeoElement copies
    // 2) Images
    // 3) Text
    // 4) CASTableCells
    // 5) GGB files
    // ------------------------------------------

    // try to get PlotPanel GeoElement copies
    if (t.isDataFlavorSupported(PlotPanelEuclidianView.plotPanelFlavor)) {

      try {
        AbstractAction act =
            (AbstractAction) t.getTransferData(PlotPanelEuclidianView.plotPanelFlavor);
        act.putValue("euclidianViewID", ev.getViewID());
        act.actionPerformed(new ActionEvent(act, 0, null));
      } catch (UnsupportedFlavorException e) {
        e.printStackTrace();
        return false;
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    // try to get an image
    boolean imageDropped = ((GuiManagerD) ev.getApplication().getGuiManager()).loadImage(t, false);
    if (imageDropped) return true;

    // handle CAS table cells as simple latex string (not dynamic!!)
    // ToDo: make it dynamic (after ticket 2449 is finished)
    DataFlavor[] df = t.getTransferDataFlavors();
    for (DataFlavor d : df) {
      App.debug(d);
    }
    if (t.isDataFlavorSupported(CASTransferHandler.casTableFlavor)) {
      try {

        // after it is possible to refer to cas cells with "$1" we can refer dynamically

        // String tableRef;
        StringBuilder sb = new StringBuilder("FormulaText[$");
        sb.append(1 + (Integer) t.getTransferData(CASTransferHandler.casTableFlavor));
        sb.append("]");
        // tableRef = "$" + (cellnumber+1);

        // create a GeoText on the specific mouse position
        GeoElement[] ret =
            ev.getApplication()
                .getKernel()
                .getAlgebraProcessor()
                .processAlgebraCommandNoExceptionHandling(sb.toString(), true, false, false);

        if (ret != null && ret[0].isTextValue()) {
          GeoText geo = (GeoText) ret[0];
          geo.setLaTeX(true, false);

          // TODO: h should equal the geo height, this is just an
          // estimate
          double h = 2 * app.getFontSize();

          geo.setRealWorldLoc(
              ev.toRealWorldCoordX(mousePos.x), ev.toRealWorldCoordY(mousePos.y - h));
          geo.updateRepaint();
        }

        return true;
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }
    }

    // check for ggb file drop
    boolean ggbFileDropped = ((GuiManagerD) app.getGuiManager()).handleGGBFileDrop(t);
    if (ggbFileDropped) return true;

    // handle all text flavors
    if (t.isDataFlavorSupported(DataFlavor.stringFlavor)
        || t.isDataFlavorSupported(AlgebraViewTransferHandler.algebraViewFlavor)) {
      try {

        String text = null; // expression to be converted into GeoText
        boolean isLaTeX = false;

        // get text from AlgebraView flavor
        if (t.isDataFlavorSupported(AlgebraViewTransferHandler.algebraViewFlavor)) {

          isLaTeX = true;

          // get list of selected geo labels
          ArrayList<String> list =
              (ArrayList<String>) t.getTransferData(AlgebraViewTransferHandler.algebraViewFlavor);

          // exit if empty list
          if (list.size() == 0) return false;

          // single geo
          if (list.size() == 1) {
            text = "FormulaText[" + list.get(0) + ", true, true]";
          }

          // multiple geos, wrap in TableText
          else {
            text = "TableText[";
            for (int i = 0; i < list.size(); i++) {

              text += "{FormulaText[" + list.get(i) + ", true, true]}";
              if (i < list.size() - 1) {
                text += ",";
              }
            }
            text += "]";
          }
        }

        // get text from String flavor
        else {
          try {
            // first try to read text line-by-line
            Reader r = textReaderFlavor.getReaderForText(t);
            if (r != null) {
              StringBuilder sb = new StringBuilder();
              String line = null;
              BufferedReader br = new BufferedReader(r);
              line = br.readLine();
              while (line != null) {
                sb.append(line + "\n");
                line = br.readLine();
              }
              br.close();
              text = sb.toString();
            }
          } catch (Exception e) {
            App.debug("Caught exception decoding text transfer:" + e.getMessage());
          }

          // if the reader didn't work, try to get whatever string is
          // available
          if (text == null) text = (String) t.getTransferData(DataFlavor.stringFlavor);

          // exit if no text found
          if (text == null) return false;

          // TODO --- validate the text? e.g. no quotes for a GeoText

          // wrap text in quotes
          text = "\"" + text + "\"";
        }

        // ---------------------------------
        // create GeoText

        GeoElement[] ret =
            ev.getApplication().getKernel().getAlgebraProcessor().processAlgebraCommand(text, true);

        if (ret != null && ret[0].isTextValue()) {
          GeoText geo = (GeoText) ret[0];
          geo.setLaTeX(isLaTeX, false);

          // TODO: h should equal the geo height, this is just an
          // estimate
          double h = 2 * app.getFontSize();

          geo.setRealWorldLoc(
              ev.toRealWorldCoordX(mousePos.x), ev.toRealWorldCoordY(mousePos.y - h));
          geo.updateRepaint();
        }

        return true;

      } catch (UnsupportedFlavorException ignored) {
        // TODO
      } catch (IOException ignored) {
        // TODO
      }
    }
    return false;
  }
Ejemplo n.º 10
0
 /** remove the start point of the wrapped GeoText */
 public static void removeTextOrigin(GeoText geo) {
   geo.removeStartPoint(geo.getStartPoint());
 }
Ejemplo n.º 11
0
 /**
  * Set the start point of the wrapped GeoText
  *
  * @param start the new start point
  * @throws CircularDefinitionException (dont' know)
  */
 public static void setTextOrigin(GeoText geo, GeoPointND start)
     throws CircularDefinitionException {
   geo.setStartPoint(start);
 }
Ejemplo n.º 12
0
 /**
  * Set whether a GeoText is LaTeX
  *
  * @param geo the GeoText object
  * @param val true if the GeoText is LaTeX
  */
 public static void setLatex(GeoText geo, boolean val) {
   geo.setLaTeX(val, false);
 }
Ejemplo n.º 13
0
 /**
  * Find whether a GeoText is LaTeX
  *
  * @param geo the GeoText object
  * @return true if the GeoText is interpreted as a LaTeX formula
  */
 public static boolean isLatex(GeoText geo) {
   return geo.isLaTeX();
 }
Ejemplo n.º 14
0
 /**
  * Set the text string of a GeoText
  *
  * @param geo the GeoText object
  * @param text the new text string
  */
 public static void setTextString(GeoText geo, String text) {
   geo.setTextString(text);
 }
Ejemplo n.º 15
0
 /**
  * Get the text string of a GeoText
  *
  * @param geo the GeoText
  * @return the text value of the GeoText
  */
 public static String getTextString(GeoText geo) {
   return geo.getTextString();
 }
Ejemplo n.º 16
0
 /** @return the origin point of the wrapped GeoText */
 public static GeoPointND getTextOrigin(GeoText geo) {
   return geo.getStartPoint();
 }