2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Manuel JULIACHS
4 * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
14 package org.scilab.modules.graphic_objects.axes;
16 import java.util.ArrayList;
18 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.UpdateStatus;
19 import org.scilab.modules.graphic_objects.textObject.Font;
20 import org.scilab.modules.graphic_objects.textObject.FormattedText;
24 * @author Manuel JULIACHS
26 public class TicksProperty {
27 /** TicksProperty properties names */
28 public enum TicksPropertyProperty { AUTO, LOCATIONS, LABELS,
29 FONT_SIZE, FONT_STYLE, FONT_COLOR, SUBTICKS
32 /** Default number of ticks */
33 private static final int DEFAULT_NUMBER_OF_TICKS = 11;
35 /** Specifies whether ticks are automatically computed or not */
38 /** Number of subticks between two main ticks */
42 private Font defaultFont;
44 private String format = "";
45 private Double[] st_factors = new Double[] {1., 0.};
47 /** TicksArrays class */
48 private class TicksArrays {
49 /** Ticks locations */
50 private double[] locations;
53 private ArrayList <FormattedText> labels;
55 /** Number of ticks */
61 public TicksArrays(int number) {
62 locations = new double[number];
63 labels = new ArrayList<FormattedText>(number);
65 for (int i = 0; i < number; i++) {
66 labels.add(i, new FormattedText());
73 public boolean equals(Object o) {
74 if (o instanceof TicksArrays) {
75 TicksArrays ta = (TicksArrays) o;
76 if (ta.number == number) {
77 for (int i = 0; i < number; i++) {
78 if (ta.locations[i] != locations[i] || !ta.labels.get(i).equals(labels.get(i))) {
91 * @return the number of ticks
93 public Integer getNumber() {
100 public ArrayList<FormattedText> getLabels() {
105 * @param labels the labels to set
107 public UpdateStatus setLabels(ArrayList<FormattedText> labels) {
108 UpdateStatus status = this.labels.equals(labels) ? UpdateStatus.NoChange : UpdateStatus.Success;
109 if (status == UpdateStatus.Success) {
110 if (!this.labels.isEmpty()) {
114 for (int i = 0; i < labels.size(); i++) {
115 this.labels.add(i, new FormattedText(labels.get(i)));
123 * @return the labels strings
125 public String[] getLabelsStrings() {
126 String[] labelsStrings;
128 labelsStrings = new String[number];
130 for (int i = 0; i < number; i++) {
131 labelsStrings[i] = new String(labels.get(i).getText()).replaceAll("\u00A0", " ");
134 return labelsStrings;
138 * Sets the ticks labels strings
139 * Requires the corresponding ticks locations to have previously been set.
140 * @param labels the labels to set
142 public UpdateStatus setLabelsStrings(String[] labels) {
143 if (labels.length != number) {
144 return UpdateStatus.NoChange;
147 if (this.labels == null || this.labels.size() != labels.length) {
148 this.labels = new ArrayList<FormattedText>(0);
150 Font font = new Font(defaultFont);
151 for (int i = 0; i < labels.length; i++) {
152 FormattedText newText = new FormattedText(labels[i], font);
153 this.labels.add(newText);
156 return UpdateStatus.Success;
159 UpdateStatus status = UpdateStatus.NoChange;
160 for (int i = 0; i < number; i++) {
161 FormattedText ft = this.labels.get(i);
162 if (!ft.getText().equals(labels[i])) {
163 this.labels.get(i).setText(labels[i]);
164 status = UpdateStatus.Success;
172 * @return the locations
174 public Double[] getLocations() {
175 Double[] retLocations;
177 retLocations = new Double[number];
179 for (int i = 0; i < number; i++) {
180 retLocations[i] = locations[i];
187 * Sets the ticks locations
188 * Also sets the current number of ticks to the size of the locations array
189 * if the latter is resized.
190 * @param locations the locations to set
192 public UpdateStatus setLocations(Double[] locations) {
193 UpdateStatus status = UpdateStatus.Success;
194 if (this.locations == null || number != locations.length) {
195 this.locations = new double[locations.length];
196 number = locations.length;
198 status = UpdateStatus.NoChange;
201 for (int i = 0; i < locations.length; i++) {
202 if (status == UpdateStatus.NoChange) {
203 if (this.locations[i] != locations[i]) {
204 status = UpdateStatus.Success;
205 this.locations[i] = locations[i];
208 this.locations[i] = locations[i];
216 * Supposes all ticks labels have the same font style.
218 * @return the ticks labels font style
220 public Integer getFontStyle() {
221 if (!labels.isEmpty()) {
222 return labels.get(0).getFont().getStyle();
228 * Supposes all ticks labels have the same font style.
230 * @param fontStyle the ticks labels font style to set
232 public UpdateStatus setFontStyle(Integer fontStyle) {
233 UpdateStatus status = UpdateStatus.NoChange;
234 for (int i = 0; i < labels.size(); i++) {
235 Font f = labels.get(i).getFont();
236 if (f.getStyle() != fontStyle) {
237 f.setStyle(fontStyle);
238 status = UpdateStatus.Success;
246 * Supposes all ticks labels have the same font size.
248 * @return the ticks labels font size
250 public Double getFontSize() {
251 if (!labels.isEmpty()) {
252 return labels.get(0).getFont().getSize();
258 * Supposes all ticks labels have the same font size.
260 * @param fontSize the ticks labels font size to set
262 public UpdateStatus setFontSize(Double fontSize) {
263 UpdateStatus status = UpdateStatus.NoChange;
264 for (int i = 0; i < labels.size(); i++) {
265 Font f = labels.get(i).getFont();
266 if (f.getSize() != fontSize) {
268 status = UpdateStatus.Success;
276 * Supposes all ticks labels have the same font color.
278 * @return the ticks labels font color
280 public Integer getFontColor() {
281 if (!labels.isEmpty()) {
282 return labels.get(0).getFont().getColor();
288 * Supposes all ticks labels have the same font color.
290 * @param fontColor the ticks labels font color to set
292 public UpdateStatus setFontColor(Integer fontColor) {
293 UpdateStatus status = UpdateStatus.NoChange;
294 for (int i = 0; i < labels.size(); i++) {
295 Font f = labels.get(i).getFont();
296 if (!f.getColor().equals(fontColor)) {
297 f.setColor(fontColor);
298 status = UpdateStatus.Success;
306 * Supposes all ticks labels have the same font fractional.
308 * @return the ticks labels font fractional
310 public Boolean getFontFractional() {
311 if (!labels.isEmpty()) {
312 return labels.get(0).getFont().getFractional();
318 * Supposes all ticks labels have the same font fractional.
320 * @param fontFractional the ticks labels font fractional to set
322 public UpdateStatus setFontFractional(Boolean fontFractional) {
323 UpdateStatus status = UpdateStatus.NoChange;
324 for (int i = 0; i < labels.size(); i++) {
325 Font f = labels.get(i).getFont();
326 if (f.getFractional() != fontFractional) {
327 f.setFractional(fontFractional);
328 status = UpdateStatus.Success;
336 /** Automatic ticks */
337 TicksArrays automaticTicks;
340 TicksArrays userTicks;
343 public TicksProperty() {
348 defaultFont = new Font();
350 automaticTicks = new TicksArrays(DEFAULT_NUMBER_OF_TICKS);
351 userTicks = new TicksArrays(0);
356 * @param ticksProperty the TicksProperty to copy
358 public TicksProperty(TicksProperty ticksProperty) {
359 auto = ticksProperty.auto;
361 subticks = ticksProperty.subticks;
362 format = ticksProperty.format;
363 st_factors = ticksProperty.st_factors;
365 defaultFont = new Font(ticksProperty.defaultFont);
367 automaticTicks = new TicksArrays(0);
368 userTicks = new TicksArrays(0);
370 automaticTicks.setLocations(ticksProperty.automaticTicks.getLocations());
371 userTicks.setLocations(ticksProperty.userTicks.getLocations());
373 automaticTicks.setLabels(ticksProperty.automaticTicks.getLabels());
374 userTicks.setLabels(ticksProperty.userTicks.getLabels());
378 public boolean equals(Object o) {
379 if (o instanceof TicksProperty) {
380 TicksProperty tp = (TicksProperty) o;
381 if (tp.auto == auto && tp.subticks == subticks && tp.defaultFont.equals(defaultFont)) {
383 return automaticTicks.equals(tp.automaticTicks);
385 return userTicks.equals(tp.userTicks);
396 public String getFormat() {
401 * @param format the format to set
403 public UpdateStatus setFormat(String format) {
404 if (!this.format.equals(format)) {
405 this.format = format;
406 return UpdateStatus.Success;
409 return UpdateStatus.NoChange;
415 public Double[] getSTFactors() {
420 * @param format the format to set
422 public UpdateStatus setSTFactors(Double[] factors) {
423 if (!this.st_factors[0].equals(factors[0]) || !this.st_factors[1].equals(factors[1])) {
424 this.st_factors = factors;
425 return UpdateStatus.Success;
428 return UpdateStatus.NoChange;
434 public Boolean getAuto() {
439 * @param auto the auto to set
441 public UpdateStatus setAuto(Boolean auto) {
442 if (this.auto != auto) {
444 return UpdateStatus.Success;
447 return UpdateStatus.NoChange;
453 public ArrayList<FormattedText> getLabels() {
455 return automaticTicks.getLabels();
457 return userTicks.getLabels();
462 * @param labels the labels to set
464 public UpdateStatus setLabels(ArrayList<FormattedText> labels) {
466 return automaticTicks.setLabels(labels);
468 return userTicks.setLabels(labels);
473 * @return the labels strings
475 public String[] getLabelsStrings() {
477 return automaticTicks.getLabelsStrings();
479 return userTicks.getLabelsStrings();
484 * Sets the ticks labels strings
485 * Requires the corresponding ticks locations to have previously been set.
486 * @param labels the labels to set
488 public UpdateStatus setLabelsStrings(String[] labels) {
490 return automaticTicks.setLabelsStrings(labels);
492 return userTicks.setLabelsStrings(labels);
497 * @return the number of ticks
499 public Integer getNumber() {
501 return automaticTicks.getNumber();
503 return userTicks.getNumber();
508 * @return the locations
510 public Double[] getLocations() {
512 return automaticTicks.getLocations();
514 return userTicks.getLocations();
519 * Sets the ticks locations
520 * Also sets the current number of ticks to the size of the locations array
521 * if the latter is resized.
522 * @param locations the locations to set
524 public UpdateStatus setLocations(Double[] locations) {
526 return automaticTicks.setLocations(locations);
528 return userTicks.setLocations(locations);
533 * @return the subticks
535 public Integer getSubticks() {
540 * @param subticks the subticks to set
542 public UpdateStatus setSubticks(Integer subticks) {
543 if (this.subticks != subticks) {
544 this.subticks = subticks;
545 return UpdateStatus.Success;
548 return UpdateStatus.NoChange;
552 * Supposes that all automatic and user ticks labels have the same font style.
553 * To be corrected (commented out block) when the associated C get function is completed.
554 * @return the ticks labels font style
556 public Integer getFontStyle() {
557 return automaticTicks.getFontStyle();
561 return automaticTicks.getFontStyle();
563 return userTicks.getFontStyle();
569 * Supposes that all automatic and user ticks labels have the same font style.
570 * To be corrected (commented out block) when the associated C set function is completed.
571 * @param fontStyle the ticks labels font style to set
573 public UpdateStatus setFontStyle(Integer fontStyle) {
574 UpdateStatus status = UpdateStatus.NoChange;
575 if (fontStyle != defaultFont.getStyle()) {
576 defaultFont.setStyle(fontStyle);
577 status = UpdateStatus.Success;
580 UpdateStatus s1 = automaticTicks.setFontStyle(fontStyle);
581 UpdateStatus s2 = userTicks.setFontStyle(fontStyle);
583 if (status == UpdateStatus.Success || s1 == UpdateStatus.Success || s2 == UpdateStatus.Success) {
584 return UpdateStatus.Success;
587 return UpdateStatus.NoChange;
591 automaticTicks.setFontStyle(fontStyle);
593 userTicks.setFontStyle(fontStyle);
599 * Supposes that all automatic and user ticks labels have the same font size.
600 * To be corrected (commented out block) when the associated C get function is completed.
601 * @return the ticks labels font size
603 public Double getFontSize() {
604 return automaticTicks.getFontSize();
608 return automaticTicks.getFontSize();
610 return userTicks.getFontSize();
616 * Supposes that all automatic and user ticks labels have the same font size.
617 * To be corrected (commented out block) when the associated C set function is completed.
618 * @param fontSize the ticks labels font size to set
620 public UpdateStatus setFontSize(Double fontSize) {
621 UpdateStatus status = UpdateStatus.NoChange;
622 if (fontSize != defaultFont.getSize()) {
623 defaultFont.setSize(fontSize);
624 status = UpdateStatus.Success;
627 UpdateStatus s1 = automaticTicks.setFontSize(fontSize);
628 UpdateStatus s2 = userTicks.setFontSize(fontSize);
630 if (status == UpdateStatus.Success || s1 == UpdateStatus.Success || s2 == UpdateStatus.Success) {
631 return UpdateStatus.Success;
634 return UpdateStatus.NoChange;
638 automaticTicks.setFontSize(fontSize);
640 userTicks.setFontSize(fontSize);
646 * Supposes that all automatic and user ticks labels have the same font color.
647 * To be corrected (commented out block) when the associated C get function is completed.
648 * @return the ticks labels font color
650 public Integer getFontColor() {
651 return automaticTicks.getFontColor();
655 return automaticTicks.getFontColor();
657 return userTicks.getFontColor();
663 * Supposes that all automatic and user ticks labels have the same font color.
664 * To be corrected (commented out block) when the associated C set function is completed.
665 * @param fontColor the ticks labels font color to set
667 public UpdateStatus setFontColor(Integer fontColor) {
668 UpdateStatus status = UpdateStatus.NoChange;
669 if (fontColor != defaultFont.getColor()) {
670 defaultFont.setColor(fontColor);
671 status = UpdateStatus.Success;
674 UpdateStatus s1 = automaticTicks.setFontColor(fontColor);
675 UpdateStatus s2 = userTicks.setFontColor(fontColor);
677 if (status == UpdateStatus.Success || s1 == UpdateStatus.Success || s2 == UpdateStatus.Success) {
678 return UpdateStatus.Success;
681 return UpdateStatus.NoChange;
685 automaticTicks.setFontColor(fontColor);
687 userTicks.setFontColor(fontColor);
693 * Supposes all automatic and user ticks labels have the same font fractional.
694 * To be corrected (commented out block) when the associated C get function is completed.
695 * @return the ticks labels font fractional
697 public Boolean getFontFractional() {
698 return automaticTicks.getFontFractional();
702 return automaticTicks.getFontFractional();
704 return userTicks.getFontFractional();
710 * Supposes all automatic and user ticks labels have the same font fractional.
711 * To be corrected (commented out block) when the associated C set function is completed.
712 * @param fontFractional the ticks labels font fractional to set
714 public UpdateStatus setFontFractional(Boolean fontFractional) {
715 UpdateStatus status = UpdateStatus.NoChange;
716 if (fontFractional != defaultFont.getFractional()) {
717 defaultFont.setFractional(fontFractional);
718 status = UpdateStatus.Success;
721 UpdateStatus s1 = automaticTicks.setFontFractional(fontFractional);
722 UpdateStatus s2 = userTicks.setFontFractional(fontFractional);
724 if (status == UpdateStatus.Success || s1 == UpdateStatus.Success || s2 == UpdateStatus.Success) {
725 return UpdateStatus.Success;
728 return UpdateStatus.NoChange;
732 automaticTicks.setFontFractional(fontFractional);
734 userTicks.setFontFractional(fontFractional);