List of usage examples for javax.swing JLabel getBorder
public Border getBorder()
null
if no border is currently set. From source file:Main.java
@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); if (c instanceof JLabel) { JLabel label = (JLabel) c; editorPane.setText(label.getText()); editorPane.setToolTipText(label.getToolTipText()); editorPane.setOpaque(label.isOpaque()); editorPane.setBackground(label.getBackground()); editorPane.setBorder(label.getBorder()); }/*w w w . j a v a 2s . c om*/ return editorPane; }
From source file:net.schweerelos.parrot.ui.GraphViewComponent.java
@SuppressWarnings("serial") private void setupRenderContext(final VisualizationViewer<NodeWrapper, NodeWrapper> vis) { vis.setRenderer(new ParrotGraphRenderer()); vis.setPickSupport(new ParrotPickSupport(vis)); RenderContext<NodeWrapper, NodeWrapper> renderContext = vis.getRenderContext(); final PickedInfo<NodeWrapper> vertexPickInfo = vis.getPickedVertexState(); final PickedState<NodeWrapper> edgePickInfo = vis.getPickedEdgeState(); // hide all edge arrows except for those on outgoing edges of picked // nodes/*from w ww .j a v a2 s .c o m*/ renderContext.setEdgeArrowPredicate(new Predicate<Context<Graph<NodeWrapper, NodeWrapper>, NodeWrapper>>() { @Override public boolean evaluate(Context<Graph<NodeWrapper, NodeWrapper>, NodeWrapper> context) { NodeWrapper edge = context.element; NodeWrapper source = graph.getSource(edge); return vertexPickInfo.isPicked(source); } }); // make edges straight lines to collapse parallel edges renderContext.setEdgeShapeTransformer(new EdgeShape.Line<NodeWrapper, NodeWrapper>()); // hide text of all edges except for outgoing edges of picked nodes renderContext.setEdgeLabelTransformer(new Transformer<NodeWrapper, String>() { @Override public String transform(NodeWrapper edge) { NodeWrapper source = graph.getSource(edge); NodeWrapper destination = graph.getDest(edge); if (vertexPickInfo.isPicked(source) && !vertexPickInfo.isPicked(destination)) { return edge.toString(); } else { return ""; } } }); renderContext.setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(COLOR_EDGE_LABEL) { @Override public <E> Component getEdgeLabelRendererComponent(JComponent vv, Object value, Font font, boolean isSelected, E edge) { Component component = super.getEdgeLabelRendererComponent(vv, value, font, isSelected, edge); component.setForeground(COLOR_EDGE_LABEL); return component; } }); // start from VertexLabelAsShapeDemo // this class will provide both label drawing and vertex shapes VertexLabelAsShapeRenderer<NodeWrapper, NodeWrapper> vlasr = new VertexLabelAsShapeRenderer<NodeWrapper, NodeWrapper>( renderContext); renderContext.setVertexShapeTransformer(vlasr); vis.setForeground(COLOR_NODE_TEXT); // customize the render context renderContext.setVertexLabelTransformer(new ToStringLabeller<NodeWrapper>()); renderContext.setVertexLabelRenderer(new DefaultVertexLabelRenderer(COLOR_NODE_PICKED_TEXT) { @Override public <V> Component getVertexLabelRendererComponent(JComponent vv, Object value, Font font, boolean isSelected, V vertexToRender) { Component component = super.getVertexLabelRendererComponent(vv, value, font, isSelected, vertexToRender); if (component instanceof JLabel) { JLabel label = (JLabel) component; // add a little bit of padding around the text Border originalBorder = label.getBorder(); label.setBorder(BorderFactory.createCompoundBorder(originalBorder, BorderFactory.createEmptyBorder(3, 2, 4, 2))); } // now set the colour/font too if (vertexToRender instanceof NodeWrapper) { NodeWrapper vertex = (NodeWrapper) vertexToRender; if (vertexPickInfo.isPicked(vertex)) { component.setForeground(COLOR_NODE_PICKED_TEXT); } else if (vertex.isHighlighted()) { component.setForeground(COLOR_NODE_HIGHLIGHTED_TEXT); component.setFont(font.deriveFont(Font.BOLD)); } else if (GraphViewHelper.hasPickedNeighbour(vertex, vertexPickInfo, graph)) { component.setForeground(COLOR_NODE_WITH_PICKED_NEIGHBOUR_TEXT); } else if (GraphViewHelper.hasPickedAdjacentEdge(vertex, edgePickInfo, graph)) { component.setForeground(COLOR_NODE_ADJACENT_EDGE_PICKED_TEXT); } else if (GraphViewHelper.hasHighlightedNeighbour(vertex, graph)) { component.setForeground(COLOR_NODE_WITH_HIGHLIGHTED_NEIGHBOUR_TEXT); } else { component.setForeground(COLOR_NODE_TEXT); } } return component; } }); // end from VertexLabelAsShapeDemo vis.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR); vis.setVertexToolTipTransformer(new Transformer<NodeWrapper, String>() { @Override public String transform(NodeWrapper vertex) { return vertex.getToolTipText(model); } }); // inspired by PluggableRendererDemo Transformer<NodeWrapper, Paint> vertexOutline = new Transformer<NodeWrapper, Paint>() { @Override public Paint transform(NodeWrapper vertex) { if (vertexPickInfo.isPicked(vertex)) { return COLOR_NODE_PICKED_BORDER; } else if (vertex.isHighlighted()) { return COLOR_NODE_HIGHLIGHTED_BORDER; } else { if (GraphViewHelper.hasPickedAdjacentEdge(vertex, edgePickInfo, graph)) { return COLOR_NODE_ADJACENT_EDGE_PICKED_BORDER; } if (GraphViewHelper.hasPickedNeighbour(vertex, vertexPickInfo, graph)) { return COLOR_NODE_WITH_PICKED_NEIGHBOUR_BORDER; } else if (GraphViewHelper.hasHighlightedNeighbour(vertex, graph)) { return COLOR_NODE_WITH_HIGHLIGHTED_NEIGHBOUR_BORDER; } // will get here only if no neighbour is picked/highlighted return COLOR_NODE_BORDER; } } }; renderContext.setVertexDrawPaintTransformer(vertexOutline); Transformer<NodeWrapper, Paint> vertexBackground = new Transformer<NodeWrapper, Paint>() { @Override public Paint transform(NodeWrapper vertex) { if (vertexPickInfo.isPicked(vertex)) { return COLOR_NODE_PICKED_BG; } else if (vertex.isHighlighted()) { return COLOR_NODE_HIGHLIGHTED_BG; } else { if (GraphViewHelper.hasPickedAdjacentEdge(vertex, edgePickInfo, graph)) { return COLOR_NODE_ADJACENT_EDGE_PICKED_BG; } if (GraphViewHelper.hasPickedNeighbour(vertex, vertexPickInfo, graph)) { return COLOR_NODE_WITH_PICKED_NEIGHBOUR_BG; } else if (GraphViewHelper.hasHighlightedNeighbour(vertex, graph)) { return COLOR_NODE_WITH_HIGHLIGHTED_NEIGHBOUR_BG; } return COLOR_NODE_BG; } } }; renderContext.setVertexFillPaintTransformer(vertexBackground); Transformer<NodeWrapper, Stroke> vertexStroke = new Transformer<NodeWrapper, Stroke>() { @Override public Stroke transform(NodeWrapper vertex) { if (vertexPickInfo.isPicked(vertex)) { return STROKE_VERTEX_PICKED; } else if (vertex.isHighlighted()) { return STROKE_VERTEX_HIGHLIGHTED; } Collection<NodeWrapper> edges = graph.getInEdges(vertex); for (NodeWrapper edge : edges) { if (edgePickInfo.isPicked(edge)) { return STROKE_VERTEX_INCOMING_EDGE_PICKED; } } edges = graph.getOutEdges(vertex); for (NodeWrapper edge : edges) { if (edgePickInfo.isPicked(edge)) { return STROKE_VERTEX_OUTGOING_EDGE_PICKED; } } // we'll only get here if none of the cases above applies return STROKE_VERTEX_DEFAULT; } }; renderContext.setVertexStrokeTransformer(vertexStroke); Transformer<NodeWrapper, Stroke> edgeStroke = new Transformer<NodeWrapper, Stroke>() { @Override public Stroke transform(NodeWrapper edge) { NodeWrapper edgeSource = graph.getSource(edge); if (edgePickInfo.isPicked(edge)) { return STROKE_EDGE_PICKED; } else if (vertexPickInfo.isPicked(edgeSource)) { return STROKE_EDGE_ADJACENT_NODE_PICKED; } else { return STROKE_EDGE_DEFAULT; } } }; renderContext.setEdgeStrokeTransformer(edgeStroke); Transformer<NodeWrapper, Paint> edgeColor = new Transformer<NodeWrapper, Paint>() { @Override public Paint transform(NodeWrapper edge) { if (edgePickInfo.isPicked(edge)) { return COLOR_EDGE_PICKED; } else if (GraphViewHelper.hasPickedAdjacentVertex(edge, vertexPickInfo, graph)) { return COLOR_EDGE_ADJACENT_VERTEX_PICKED; } else if (edge.isHighlighted()) { return COLOR_EDGE_HIGHLIGHTED; } else if (GraphViewHelper.hasHighlightedAdjacentVertex(edge, graph)) { return COLOR_EDGE_ADJACENT_VERTEX_HIGHLIGHTED; } else { return COLOR_EDGE; } } }; renderContext.setEdgeDrawPaintTransformer(edgeColor); // draw arrows in the same colour as edges renderContext.setArrowDrawPaintTransformer(edgeColor); renderContext.setArrowFillPaintTransformer(edgeColor); includePredicate = new IncludePredicate<Context<Graph<NodeWrapper, NodeWrapper>, NodeWrapper>>(); renderContext.setEdgeIncludePredicate(includePredicate); renderContext.setVertexIncludePredicate(includePredicate); vis.setBackground(COLOR_BACKGROUND); mouse = new DoubleClickPickingModalGraphMouse<NodeWrapper, NodeWrapper>(); mouse.add(new AbstractPopupGraphMousePlugin() { @Override protected void handlePopup(MouseEvent e) { if (!e.isPopupTrigger()) { return; } GraphElementAccessor<NodeWrapper, NodeWrapper> pickSupport = vis.getPickSupport(); if (pickSupport == null) { return; } NodeWrapper node = pickSupport.getVertex(layout, e.getX(), e.getY()); if (node == null) { node = pickSupport.getEdge(layout, e.getX(), e.getY()); } if (node == null) { return; } popup.setNodeWrapper(node); popup.show(vis, e.getX(), e.getY()); } }); mouse.setDoubleClickPickingPlugin(new DoubleClickPickingPlugin() { @Override void doubleClickOccurred(MouseEvent e) { GraphElementAccessor<NodeWrapper, NodeWrapper> pickSupport = vis.getPickSupport(); if (pickSupport == null) { return; } NodeWrapper node = pickSupport.getVertex(layout, e.getX(), e.getY()); if (node == null) { return; } fireNodeSelected(node); } }); vis.setGraphMouse(mouse); }
From source file:com.intel.stl.ui.common.view.ComponentFactory.java
/** * /*w ww . j a v a 2s . c o m*/ * <i>Description:</i> simple method that creates single/multi line label * with specified label width based on a source label. For more advanced * label attributes, it's the developer's responsibility to set them back. * * @param source * @param wrap * @param maxWidth * @return */ public static JLabel deriveLabel(JLabel source, final boolean wrap, final int maxWidth) { JXLabel label = new JXLabel(source.getText(), source.getIcon(), source.getHorizontalAlignment()) { private static final long serialVersionUID = -4816144910055350011L; private Font cachedFont; private String chahedRawText, chahedText; /* * (non-Javadoc) * * @see javax.swing.JLabel#getText() */ @Override public String getText() { String text = super.getText(); if (wrap || maxWidth <= 0 || text == null || text.isEmpty()) { return text; } if (getFont().equals(cachedFont) && text.equals(chahedRawText)) { return chahedText; } chahedRawText = text; cachedFont = getFont(); FontMetrics fm = getFontMetrics(cachedFont); char[] chars = text.toCharArray(); int width = fm.charsWidth(chars, 0, chars.length); if (width < maxWidth) { chahedText = text; } else { width += fm.charWidth('.') * 3; int pos = chars.length - 1; for (; pos >= 0 && width > maxWidth; pos--) { width -= fm.charWidth(chars[pos]); } chahedText = new String(chars, 0, pos) + "..."; if (getToolTipText() == null) { setToolTipText(text); } } return chahedText; } }; if (wrap) { label.setLineWrap(true); } if (maxWidth > 0) { label.setMaxLineSpan(maxWidth); } label.setEnabled(source.isEnabled()); label.setForeground(source.getForeground()); label.setOpaque(source.isOpaque()); label.setBackground(source.getBackground()); label.setFont(source.getFont()); label.setBorder(source.getBorder()); label.setToolTipText(source.getToolTipText()); return label; }