Source Code Cross Referenced for SynthTreeUI.java in  » 6.0-JDK-Core » swing » javax » swing » plaf » synth » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing.plaf.synth 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2002-2007 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025        package javax.swing.plaf.synth;
026
027        import javax.swing.*;
028        import javax.swing.event.*;
029        import java.awt.*;
030        import java.awt.event.*;
031        import java.awt.datatransfer.*;
032        import java.awt.dnd.*;
033        import java.beans.*;
034        import java.io.*;
035        import java.util.*;
036        import javax.swing.plaf.*;
037        import javax.swing.plaf.basic.*;
038        import javax.swing.tree.*;
039        import javax.swing.text.Position;
040        import sun.swing.plaf.synth.*;
041
042        /**
043         * Skinnable TreeUI.
044         *
045         * @version 1.40, 05/05/07
046         * @author Scott Violet
047         */
048        class SynthTreeUI extends BasicTreeUI implements 
049                PropertyChangeListener, SynthUI {
050            private SynthStyle style;
051            private SynthStyle cellStyle;
052
053            private SynthContext paintContext;
054
055            private boolean drawHorizontalLines;
056            private boolean drawVerticalLines;
057
058            private Object linesStyle;
059
060            private int leadRow;
061
062            private int padding;
063
064            private boolean useTreeColors;
065
066            private Icon expandedIconWrapper;
067
068            public static ComponentUI createUI(JComponent x) {
069                return new SynthTreeUI();
070            }
071
072            SynthTreeUI() {
073                expandedIconWrapper = new ExpandedIconWrapper();
074            }
075
076            public Icon getExpandedIcon() {
077                return expandedIconWrapper;
078            }
079
080            protected void installDefaults() {
081                updateStyle(tree);
082            }
083
084            private void updateStyle(JTree tree) {
085                SynthContext context = getContext(tree, ENABLED);
086                SynthStyle oldStyle = style;
087
088                style = SynthLookAndFeel.updateStyle(context, this );
089                if (style != oldStyle) {
090                    Object value;
091
092                    setExpandedIcon(style.getIcon(context, "Tree.expandedIcon"));
093                    setCollapsedIcon(style.getIcon(context,
094                            "Tree.collapsedIcon"));
095
096                    setLeftChildIndent(style.getInt(context,
097                            "Tree.leftChildIndent", 0));
098                    setRightChildIndent(style.getInt(context,
099                            "Tree.rightChildIndent", 0));
100
101                    drawHorizontalLines = style.getBoolean(context,
102                            "Tree.drawHorizontalLines", true);
103                    drawVerticalLines = style.getBoolean(context,
104                            "Tree.drawVerticalLines", true);
105                    linesStyle = style.get(context, "Tree.linesStyle");
106
107                    value = style.get(context, "Tree.rowHeight");
108                    if (value != null) {
109                        LookAndFeel.installProperty(tree, "rowHeight", value);
110                    }
111
112                    value = style.get(context, "Tree.scrollsOnExpand");
113                    LookAndFeel.installProperty(tree, "scrollsOnExpand",
114                            value != null ? value : Boolean.TRUE);
115
116                    padding = style.getInt(context, "Tree.padding", 0);
117
118                    largeModel = (tree.isLargeModel() && tree.getRowHeight() > 0);
119
120                    useTreeColors = style.getBoolean(context,
121                            "Tree.rendererUseTreeColors", true);
122
123                    Boolean showsRootHandles = style.getBoolean(context,
124                            "Tree.showsRootHandles", Boolean.TRUE);
125                    LookAndFeel
126                            .installProperty(tree,
127                                    JTree.SHOWS_ROOT_HANDLES_PROPERTY,
128                                    showsRootHandles);
129
130                    if (oldStyle != null) {
131                        uninstallKeyboardActions();
132                        installKeyboardActions();
133                    }
134                }
135                context.dispose();
136
137                context = getContext(tree, Region.TREE_CELL, ENABLED);
138                cellStyle = SynthLookAndFeel.updateStyle(context, this );
139                context.dispose();
140            }
141
142            protected void installListeners() {
143                super .installListeners();
144                tree.addPropertyChangeListener(this );
145            }
146
147            public SynthContext getContext(JComponent c) {
148                return getContext(c, getComponentState(c));
149            }
150
151            private SynthContext getContext(JComponent c, int state) {
152                return SynthContext.getContext(SynthContext.class, c,
153                        SynthLookAndFeel.getRegion(c), style, state);
154            }
155
156            private Region getRegion(JTree c) {
157                return SynthLookAndFeel.getRegion(c);
158            }
159
160            private int getComponentState(JComponent c) {
161                return SynthLookAndFeel.getComponentState(c);
162            }
163
164            private SynthContext getContext(JComponent c, Region region) {
165                return getContext(c, region, getComponentState(c, region));
166            }
167
168            private SynthContext getContext(JComponent c, Region region,
169                    int state) {
170                return SynthContext.getContext(SynthContext.class, c, region,
171                        cellStyle, state);
172            }
173
174            private int getComponentState(JComponent c, Region region) {
175                // Always treat the cell as selected, will be adjusted appropriately
176                // when painted.
177                return ENABLED | SELECTED;
178            }
179
180            protected TreeCellEditor createDefaultCellEditor() {
181                TreeCellRenderer renderer = tree.getCellRenderer();
182                DefaultTreeCellEditor editor;
183
184                if (renderer != null
185                        && (renderer instanceof  DefaultTreeCellRenderer)) {
186                    editor = new SynthTreeCellEditor(tree,
187                            (DefaultTreeCellRenderer) renderer);
188                } else {
189                    editor = new SynthTreeCellEditor(tree, null);
190                }
191                return editor;
192            }
193
194            protected TreeCellRenderer createDefaultCellRenderer() {
195                return new SynthTreeCellRenderer();
196            }
197
198            protected void uninstallDefaults() {
199                SynthContext context = getContext(tree, ENABLED);
200
201                style.uninstallDefaults(context);
202                context.dispose();
203                style = null;
204
205                context = getContext(tree, Region.TREE_CELL, ENABLED);
206                cellStyle.uninstallDefaults(context);
207                context.dispose();
208                cellStyle = null;
209
210                if (tree.getTransferHandler() instanceof  UIResource) {
211                    tree.setTransferHandler(null);
212                }
213            }
214
215            protected void uninstallListeners() {
216                super .uninstallListeners();
217                tree.removePropertyChangeListener(this );
218            }
219
220            public void update(Graphics g, JComponent c) {
221                SynthContext context = getContext(c);
222
223                SynthLookAndFeel.update(context, g);
224                context.getPainter().paintTreeBackground(context, g, 0, 0,
225                        c.getWidth(), c.getHeight());
226                paint(context, g);
227                context.dispose();
228            }
229
230            public void paintBorder(SynthContext context, Graphics g, int x,
231                    int y, int w, int h) {
232                context.getPainter().paintTreeBorder(context, g, x, y, w, h);
233            }
234
235            public void paint(Graphics g, JComponent c) {
236                SynthContext context = getContext(c);
237
238                paint(context, g);
239                context.dispose();
240            }
241
242            private void updateLeadRow() {
243                leadRow = getRowForPath(tree, tree.getLeadSelectionPath());
244            }
245
246            protected void paint(SynthContext context, Graphics g) {
247                paintContext = context;
248
249                updateLeadRow();
250
251                Rectangle paintBounds = g.getClipBounds();
252                Insets insets = tree.getInsets();
253                TreePath initialPath = getClosestPathForLocation(tree, 0,
254                        paintBounds.y);
255                Enumeration paintingEnumerator = treeState
256                        .getVisiblePathsFrom(initialPath);
257                int row = treeState.getRowForPath(initialPath);
258                int endY = paintBounds.y + paintBounds.height;
259                TreeModel treeModel = tree.getModel();
260                SynthContext cellContext = getContext(tree, Region.TREE_CELL);
261
262                drawingCache.clear();
263
264                setHashColor(context.getStyle().getColor(context,
265                        ColorType.FOREGROUND));
266
267                if (paintingEnumerator != null) {
268                    // First pass, draw the rows
269
270                    boolean done = false;
271                    boolean isExpanded;
272                    boolean hasBeenExpanded;
273                    boolean isLeaf;
274                    Rectangle boundsBuffer = new Rectangle();
275                    Rectangle rowBounds = new Rectangle(0, 0, tree.getWidth(),
276                            0);
277                    Rectangle bounds;
278                    TreePath path;
279                    TreeCellRenderer renderer = tree.getCellRenderer();
280                    DefaultTreeCellRenderer dtcr = (renderer instanceof  DefaultTreeCellRenderer) ? (DefaultTreeCellRenderer) renderer
281                            : null;
282
283                    configureRenderer(cellContext);
284                    while (!done && paintingEnumerator.hasMoreElements()) {
285                        path = (TreePath) paintingEnumerator.nextElement();
286                        if (path != null) {
287                            isLeaf = treeModel.isLeaf(path
288                                    .getLastPathComponent());
289                            if (isLeaf) {
290                                isExpanded = hasBeenExpanded = false;
291                            } else {
292                                isExpanded = treeState.getExpandedState(path);
293                                hasBeenExpanded = tree.hasBeenExpanded(path);
294                            }
295                            bounds = getPathBounds(tree, path);
296                            rowBounds.y = bounds.y;
297                            rowBounds.height = bounds.height;
298                            paintRow(renderer, dtcr, context, cellContext, g,
299                                    paintBounds, insets, bounds, rowBounds,
300                                    path, row, isExpanded, hasBeenExpanded,
301                                    isLeaf);
302                            if ((bounds.y + bounds.height) >= endY) {
303                                done = true;
304                            }
305                        } else {
306                            done = true;
307                        }
308                        row++;
309                    }
310
311                    // Draw the connecting lines and controls.
312                    // Find each parent and have them draw a line to their last child
313                    boolean rootVisible = tree.isRootVisible();
314                    TreePath parentPath = initialPath;
315                    parentPath = parentPath.getParentPath();
316                    while (parentPath != null) {
317                        paintVerticalPartOfLeg(g, paintBounds, insets,
318                                parentPath);
319                        drawingCache.put(parentPath, Boolean.TRUE);
320                        parentPath = parentPath.getParentPath();
321                    }
322                    done = false;
323                    paintingEnumerator = treeState
324                            .getVisiblePathsFrom(initialPath);
325                    while (!done && paintingEnumerator.hasMoreElements()) {
326                        path = (TreePath) paintingEnumerator.nextElement();
327                        if (path != null) {
328                            isLeaf = treeModel.isLeaf(path
329                                    .getLastPathComponent());
330                            if (isLeaf) {
331                                isExpanded = hasBeenExpanded = false;
332                            } else {
333                                isExpanded = treeState.getExpandedState(path);
334                                hasBeenExpanded = tree.hasBeenExpanded(path);
335                            }
336                            bounds = getPathBounds(tree, path);
337                            // See if the vertical line to the parent has been drawn.
338                            parentPath = path.getParentPath();
339                            if (parentPath != null) {
340                                if (drawingCache.get(parentPath) == null) {
341                                    paintVerticalPartOfLeg(g, paintBounds,
342                                            insets, parentPath);
343                                    drawingCache.put(parentPath, Boolean.TRUE);
344                                }
345                                paintHorizontalPartOfLeg(g, paintBounds,
346                                        insets, bounds, path, row, isExpanded,
347                                        hasBeenExpanded, isLeaf);
348                            } else if (rootVisible && row == 0) {
349                                paintHorizontalPartOfLeg(g, paintBounds,
350                                        insets, bounds, path, row, isExpanded,
351                                        hasBeenExpanded, isLeaf);
352                            }
353                            if (shouldPaintExpandControl(path, row, isExpanded,
354                                    hasBeenExpanded, isLeaf)) {
355                                paintExpandControl(g, paintBounds, insets,
356                                        bounds, path, row, isExpanded,
357                                        hasBeenExpanded, isLeaf);
358                            }
359                            if ((bounds.y + bounds.height) >= endY) {
360                                done = true;
361                            }
362                        } else {
363                            done = true;
364                        }
365                        row++;
366                    }
367                }
368                cellContext.dispose();
369
370                paintDropLine(g);
371
372                // Empty out the renderer pane, allowing renderers to be gc'ed.
373                rendererPane.removeAll();
374            }
375
376            private boolean isDropLine(JTree.DropLocation loc) {
377                return loc != null && loc.getPath() != null
378                        && loc.getChildIndex() != -1;
379            }
380
381            private void paintDropLine(Graphics g) {
382                JTree.DropLocation loc = tree.getDropLocation();
383                if (!isDropLine(loc)) {
384                    return;
385                }
386
387                Color c = (Color) style.get(paintContext, "Tree.dropLineColor");
388                if (c != null) {
389                    g.setColor(c);
390                    Rectangle rect = getDropLineRect(loc);
391                    g.fillRect(rect.x, rect.y, rect.width, rect.height);
392                }
393            }
394
395            private Rectangle getDropLineRect(JTree.DropLocation loc) {
396                Rectangle rect = null;
397                TreePath path = loc.getPath();
398                int index = loc.getChildIndex();
399                boolean ltr = tree.getComponentOrientation().isLeftToRight();
400
401                Insets insets = tree.getInsets();
402
403                if (tree.getRowCount() == 0) {
404                    rect = new Rectangle(insets.left, insets.top, tree
405                            .getWidth()
406                            - insets.left - insets.right, 0);
407                } else {
408                    int row = tree.getRowForPath(path);
409                    TreeModel model = getModel();
410                    Object root = model.getRoot();
411
412                    if (path.getLastPathComponent() == root
413                            && index >= model.getChildCount(root)) {
414
415                        rect = tree.getRowBounds(tree.getRowCount() - 1);
416                        rect.y = rect.y + rect.height;
417                        Rectangle xRect;
418
419                        if (!tree.isRootVisible()) {
420                            xRect = tree.getRowBounds(0);
421                        } else if (model.getChildCount(root) == 0) {
422                            xRect = tree.getRowBounds(0);
423                            xRect.x += totalChildIndent;
424                            xRect.width -= totalChildIndent + totalChildIndent;
425                        } else {
426                            TreePath lastChildPath = path
427                                    .pathByAddingChild(model.getChild(root,
428                                            model.getChildCount(root) - 1));
429                            xRect = tree.getPathBounds(lastChildPath);
430                        }
431
432                        rect.x = xRect.x;
433                        rect.width = xRect.width;
434                    } else {
435                        rect = tree.getPathBounds(path.pathByAddingChild(model
436                                .getChild(path.getLastPathComponent(), index)));
437                    }
438                }
439
440                if (rect.y != 0) {
441                    rect.y--;
442                }
443
444                if (!ltr) {
445                    rect.x = rect.x + rect.width - 100;
446                }
447
448                rect.width = 100;
449                rect.height = 2;
450
451                return rect;
452            }
453
454            private void configureRenderer(SynthContext context) {
455                TreeCellRenderer renderer = tree.getCellRenderer();
456
457                if (renderer instanceof  DefaultTreeCellRenderer) {
458                    DefaultTreeCellRenderer r = (DefaultTreeCellRenderer) renderer;
459                    SynthStyle style = context.getStyle();
460
461                    context.setComponentState(ENABLED | SELECTED);
462                    Color color = r.getTextSelectionColor();
463                    if (color == null || (color instanceof  UIResource)) {
464                        r.setTextSelectionColor(style.getColor(context,
465                                ColorType.TEXT_FOREGROUND));
466                    }
467                    color = r.getBackgroundSelectionColor();
468                    if (color == null || (color instanceof  UIResource)) {
469                        r.setBackgroundSelectionColor(style.getColor(context,
470                                ColorType.TEXT_BACKGROUND));
471                    }
472
473                    context.setComponentState(ENABLED);
474                    color = r.getTextNonSelectionColor();
475                    if (color == null || color instanceof  UIResource) {
476                        r.setTextNonSelectionColor(style.getColorForState(
477                                context, ColorType.TEXT_FOREGROUND));
478                    }
479                    color = r.getBackgroundNonSelectionColor();
480                    if (color == null || color instanceof  UIResource) {
481                        r.setBackgroundNonSelectionColor(style
482                                .getColorForState(context,
483                                        ColorType.TEXT_BACKGROUND));
484                    }
485                }
486            }
487
488            protected void paintHorizontalPartOfLeg(Graphics g,
489                    Rectangle clipBounds, Insets insets, Rectangle bounds,
490                    TreePath path, int row, boolean isExpanded,
491                    boolean hasBeenExpanded, boolean isLeaf) {
492                if (drawHorizontalLines) {
493                    super .paintHorizontalPartOfLeg(g, clipBounds, insets,
494                            bounds, path, row, isExpanded, hasBeenExpanded,
495                            isLeaf);
496                }
497            }
498
499            protected void paintHorizontalLine(Graphics g, JComponent c, int y,
500                    int left, int right) {
501                paintContext.getStyle().getGraphicsUtils(paintContext)
502                        .drawLine(paintContext, "Tree.horizontalLine", g, left,
503                                y, right, y, linesStyle);
504            }
505
506            protected void paintVerticalPartOfLeg(Graphics g,
507                    Rectangle clipBounds, Insets insets, TreePath path) {
508                if (drawVerticalLines) {
509                    super .paintVerticalPartOfLeg(g, clipBounds, insets, path);
510                }
511            }
512
513            protected void paintVerticalLine(Graphics g, JComponent c, int x,
514                    int top, int bottom) {
515                paintContext.getStyle().getGraphicsUtils(paintContext)
516                        .drawLine(paintContext, "Tree.verticalLine", g, x, top,
517                                x, bottom, linesStyle);
518            }
519
520            protected void paintRow(TreeCellRenderer renderer,
521                    DefaultTreeCellRenderer dtcr, SynthContext treeContext,
522                    SynthContext cellContext, Graphics g, Rectangle clipBounds,
523                    Insets insets, Rectangle bounds, Rectangle rowBounds,
524                    TreePath path, int row, boolean isExpanded,
525                    boolean hasBeenExpanded, boolean isLeaf) {
526                // Don't paint the renderer if editing this row.
527                boolean selected = tree.isRowSelected(row);
528
529                JTree.DropLocation dropLocation = (JTree.DropLocation) tree
530                        .getDropLocation();
531                boolean isDrop = dropLocation != null
532                        && dropLocation.getChildIndex() == -1
533                        && path == dropLocation.getPath();
534
535                if (selected || isDrop) {
536                    cellContext.setComponentState(ENABLED | SELECTED);
537                } else {
538                    cellContext.setComponentState(ENABLED);
539                }
540                if (dtcr != null
541                        && (dtcr.getBorderSelectionColor() instanceof  UIResource)) {
542                    dtcr.setBorderSelectionColor(style.getColor(cellContext,
543                            ColorType.FOCUS));
544                }
545                SynthLookAndFeel.updateSubregion(cellContext, g, rowBounds);
546                cellContext.getPainter().paintTreeCellBackground(cellContext,
547                        g, rowBounds.x, rowBounds.y, rowBounds.width,
548                        rowBounds.height);
549                cellContext.getPainter().paintTreeCellBorder(cellContext, g,
550                        rowBounds.x, rowBounds.y, rowBounds.width,
551                        rowBounds.height);
552                if (editingComponent != null && editingRow == row) {
553                    return;
554                }
555
556                int leadIndex;
557
558                if (tree.hasFocus()) {
559                    leadIndex = leadRow;
560                } else {
561                    leadIndex = -1;
562                }
563
564                Component component = renderer.getTreeCellRendererComponent(
565                        tree, path.getLastPathComponent(), selected,
566                        isExpanded, isLeaf, row, (leadIndex == row));
567
568                rendererPane.paintComponent(g, component, tree, bounds.x,
569                        bounds.y, bounds.width, bounds.height, true);
570            }
571
572            private int findCenteredX(int x, int iconWidth) {
573                return tree.getComponentOrientation().isLeftToRight() ? x
574                        - (int) Math.ceil(iconWidth / 2.0) : x
575                        - (int) Math.floor(iconWidth / 2.0);
576            }
577
578            protected void drawCentered(Component c, Graphics graphics,
579                    Icon icon, int x, int y) {
580                int w = SynthIcon.getIconWidth(icon, paintContext);
581                int h = SynthIcon.getIconHeight(icon, paintContext);
582
583                SynthIcon.paintIcon(icon, paintContext, graphics,
584                        findCenteredX(x, w), y - h / 2, w, h);
585            }
586
587            public void propertyChange(PropertyChangeEvent event) {
588                if (SynthLookAndFeel.shouldUpdateStyle(event)) {
589                    updateStyle((JTree) event.getSource());
590                }
591
592                if ("dropLocation" == event.getPropertyName()) {
593                    JTree.DropLocation oldValue = (JTree.DropLocation) event
594                            .getOldValue();
595                    repaintDropLocation(oldValue);
596                    repaintDropLocation(tree.getDropLocation());
597                }
598            }
599
600            private void repaintDropLocation(JTree.DropLocation loc) {
601                if (loc == null) {
602                    return;
603                }
604
605                Rectangle r;
606
607                if (isDropLine(loc)) {
608                    r = getDropLineRect(loc);
609                } else {
610                    r = tree.getPathBounds(loc.getPath());
611                    if (r != null) {
612                        r.x = 0;
613                        r.width = tree.getWidth();
614                    }
615                }
616
617                if (r != null) {
618                    tree.repaint(r);
619                }
620            }
621
622            protected int getRowX(int row, int depth) {
623                return super .getRowX(row, depth) + padding;
624            }
625
626            private class SynthTreeCellRenderer extends DefaultTreeCellRenderer
627                    implements  UIResource {
628                SynthTreeCellRenderer() {
629                }
630
631                public String getName() {
632                    return "Tree.cellRenderer";
633                }
634
635                public Component getTreeCellRendererComponent(JTree tree,
636                        Object value, boolean sel, boolean expanded,
637                        boolean leaf, int row, boolean hasFocus) {
638                    if (!useTreeColors && (sel || hasFocus)) {
639                        SynthLookAndFeel.setSelectedUI(
640                                (SynthLabelUI) SynthLookAndFeel.getUIOfType(
641                                        getUI(), SynthLabelUI.class), sel,
642                                hasFocus, tree.isEnabled(), false);
643                    } else {
644                        SynthLookAndFeel.resetSelectedUI();
645                    }
646                    return super .getTreeCellRendererComponent(tree, value, sel,
647                            expanded, leaf, row, hasFocus);
648                }
649
650                public void paint(Graphics g) {
651                    paintComponent(g);
652                    if (hasFocus) {
653                        SynthContext context = getContext(tree,
654                                Region.TREE_CELL);
655
656                        if (context.getStyle() == null) {
657                            assert false : "SynthTreeCellRenderer is being used "
658                                    + "outside of UI that created it";
659                            return;
660                        }
661                        int imageOffset = 0;
662                        Icon currentI = getIcon();
663
664                        if (currentI != null && getText() != null) {
665                            imageOffset = currentI.getIconWidth()
666                                    + Math.max(0, getIconTextGap() - 1);
667                        }
668                        if (selected) {
669                            context.setComponentState(ENABLED | SELECTED);
670                        } else {
671                            context.setComponentState(ENABLED);
672                        }
673                        if (getComponentOrientation().isLeftToRight()) {
674                            context.getPainter().paintTreeCellFocus(context, g,
675                                    imageOffset, 0, getWidth() - imageOffset,
676                                    getHeight());
677                        } else {
678                            context.getPainter()
679                                    .paintTreeCellFocus(context, g, 0, 0,
680                                            getWidth() - imageOffset,
681                                            getHeight());
682                        }
683                        context.dispose();
684                    }
685                    SynthLookAndFeel.resetSelectedUI();
686                }
687            }
688
689            private static class SynthTreeCellEditor extends
690                    DefaultTreeCellEditor {
691                public SynthTreeCellEditor(JTree tree,
692                        DefaultTreeCellRenderer renderer) {
693                    super (tree, renderer);
694                    setBorderSelectionColor(null);
695                }
696
697                protected TreeCellEditor createTreeCellEditor() {
698                    JTextField tf = new JTextField() {
699                        public String getName() {
700                            return "Tree.cellEditor";
701                        }
702                    };
703                    DefaultCellEditor editor = new DefaultCellEditor(tf);
704
705                    // One click to edit.
706                    editor.setClickCountToStart(1);
707                    return editor;
708                }
709            }
710
711            //
712            // BasicTreeUI directly uses expandIcon outside of the Synth methods.
713            // To get the correct context we return an instance of this that fetches
714            // the SynthContext as needed.
715            //
716            private class ExpandedIconWrapper extends SynthIcon {
717                public void paintIcon(SynthContext context, Graphics g, int x,
718                        int y, int w, int h) {
719                    if (context == null) {
720                        context = getContext(tree);
721                        SynthIcon.paintIcon(expandedIcon, context, g, x, y, w,
722                                h);
723                        context.dispose();
724                    } else {
725                        SynthIcon.paintIcon(expandedIcon, context, g, x, y, w,
726                                h);
727                    }
728                }
729
730                public int getIconWidth(SynthContext context) {
731                    int width;
732                    if (context == null) {
733                        context = getContext(tree);
734                        width = SynthIcon.getIconWidth(expandedIcon, context);
735                        context.dispose();
736                    } else {
737                        width = SynthIcon.getIconWidth(expandedIcon, context);
738                    }
739                    return width;
740                }
741
742                public int getIconHeight(SynthContext context) {
743                    int height;
744                    if (context == null) {
745                        context = getContext(tree);
746                        height = SynthIcon.getIconHeight(expandedIcon, context);
747                        context.dispose();
748                    } else {
749                        height = SynthIcon.getIconHeight(expandedIcon, context);
750                    }
751                    return height;
752                }
753            }
754        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.