Source code: org/gendiapo/editor/document/NodeSpecialView.java
1 /*
2 * @(#)NodeSpecialView.java 1.000 01/05/30
3 */
4 package org.gendiapo.editor.document;
5
6 import java.util.Vector;
7 import java.util.Properties;
8 import java.awt.*;
9 import javax.swing.event.*;
10 import javax.swing.text.*;
11 import javax.swing.*;
12
13 import org.merlotxml.merlot.*;
14 import org.gendiapo.editor.*;
15
16 public class NodeSpecialView extends AbstractNodeView {
17
18 protected int width;
19
20 protected int theFontSize;
21 protected Font theFont;
22 protected String theText;
23
24 public NodeSpecialView(Element elem, int width) {
25 super(elem, View.Y_AXIS);
26 // Calcul la profondeur du noeud
27 int depth = 0;
28 Element e = elem;
29 while (e != null) {
30 e = e.getParentElement();
31 depth++;
32 }
33 // Defini la taille de la police en fonction de la profondeur du noeud
34 int size = 19 - (depth / 2);
35 size = (size<12)?12:size;
36 init(elem, size, width);
37 }
38
39 public NodeSpecialView(Element elem, int fontSize, int width) {
40 super(elem, View.Y_AXIS);
41 init(elem, fontSize, width);
42 }
43
44 protected void init(Element elem, int fontSize, int width) {
45 // width
46 this.width = width;
47 //
48 setInsets((short) 0, (short) 0, (short) 2, (short) 0);
49 // theFontSize
50 theFontSize = fontSize;
51 // theFont
52 theFont = ConstantView.getNodeFont(theFontSize);
53 // theText
54 MerlotDOMEditor editor = node.getEditor();
55 if (editor instanceof GenDiapoEditor) {
56 theText = ((GenDiapoEditor) editor).getUserName(node);
57 } else {
58 theText = node.getNodeName();
59 }
60
61 }
62
63 public void paintSelected(Graphics g, Color c) {
64 Rectangle r = shape.getBounds();
65 Color old = g.getColor();
66 g.setColor(c);
67 g.drawRect(r.x, r.y, r.width, r.height);
68 g.fillRect(r.x, r.y, width, r.height);
69 g.setColor(old);
70 }
71
72 public void paint(Graphics g, Shape a) {
73 Rectangle r = a.getBounds();
74
75 super.paint(g,a);
76
77 Font oldFont = g.getFont();
78
79 g.setFont(theFont);
80 g.drawString(theText,r.x+2,r.y+theFontSize+2);
81 g.setFont(oldFont);
82 }
83
84
85 protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) {
86 // calculate tiled request
87 float min = 0;
88 float pref = 0;
89 float max = 0;
90
91 // don't use first if more than one child
92 int n = getViewCount();
93 int first = n>1?1:0;
94 for (int i = first; i < n; i++) {
95 View v = getView(i);
96 min += v.getMinimumSpan(axis);
97 pref += v.getPreferredSpan(axis);
98 max += v.getMaximumSpan(axis);
99 }
100
101 if (r == null) {
102 r = new SizeRequirements();
103 }
104 r.alignment = 0.5f;
105 r.minimum = (int) min;
106 r.preferred = (int) pref;
107 r.maximum = (int) max;
108 return r;
109 }
110
111
112 /*
113 layoutMinorAxis
114 Force l'alignement a gauche
115 */
116 protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
117 super.layoutMinorAxis(targetSpan, axis, offsets, spans);
118 int n = getViewCount();
119 // don't use first if more than one child
120 if (n>1) {
121 offsets[0] = 0;
122 spans[0] = width;
123 }
124 for (int i = 1; i < n; i++) {
125 offsets[i] = width;
126 spans[i] -= width;
127 }
128 }
129
130 protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
131 /*
132 * first pass, calculate the preferred sizes
133 * and the flexibility to adjust the sizes.
134 */
135 long minimum = 0;
136 long maximum = 0;
137 long preferred = 0;
138 int n = getViewCount();
139 for (int i = 0; i < n; i++) {
140 View v = getView(i);
141 spans[i] = (int) v.getPreferredSpan(axis);
142 if (!(n>1 && i==0)) {
143 preferred += spans[i];
144 minimum += v.getMinimumSpan(axis);
145 maximum += v.getMaximumSpan(axis);
146 }
147 }
148
149 /*
150 * Second pass, expand or contract by as much as possible to reach
151 * the target span.
152 */
153
154 // determine the adjustment to be made
155 long desiredAdjustment = targetSpan - preferred;
156 float adjustmentFactor = 0.0f;
157 if (desiredAdjustment != 0) {
158 float maximumAdjustment = (desiredAdjustment > 0) ?
159 maximum - preferred : preferred - minimum;
160 if (maximumAdjustment == 0.0f) {
161 adjustmentFactor = 0.0f;
162 }
163 else {
164 adjustmentFactor = desiredAdjustment / maximumAdjustment;
165 adjustmentFactor = Math.min(adjustmentFactor, 1.0f);
166 adjustmentFactor = Math.max(adjustmentFactor, -1.0f);
167 }
168 }
169
170 // make the adjustments
171 int totalOffset = 0;
172
173 // force first
174 if (n<=1) {
175 spans[0] = 18;
176 } else {
177 offsets[0] = 0;
178 }
179 for (int i = 1; i < n; i++) {
180 View v = getView(i);
181 offsets[i] = totalOffset;
182 int availableSpan = (adjustmentFactor > 0.0f) ?
183 (int) v.getMaximumSpan(axis) - spans[i] :
184 spans[i] - (int) v.getMinimumSpan(axis);
185 float adjF = adjustmentFactor * availableSpan;
186 if (adjF < 0) {
187 adjF -= .5f;
188 }
189 else {
190 adjF += .5f;
191 }
192 int adj = (int)adjF;
193 spans[i] += adj;
194 totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
195 }
196 }
197
198
199 }