Java Code Examples for javax.swing.JLabel#getClientProperty()

The following examples show how to use javax.swing.JLabel#getClientProperty() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private static void testAccessibleProperty() {
    String text = "html text";
    JLabel testLabel = new JLabel("<html>" + text + "</html>");
    if (!text.equals(testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY))) {
        throw new RuntimeException("Incorrect ACCESSIBLE_NAME_PROPERTY," +
            " Expected: " + text +
            " Actual: " + testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY));
    }

    String namePropertyText = "name property";
    testLabel.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, namePropertyText);
    if (!namePropertyText.equals(testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY))) {
        throw new RuntimeException("Incorrect ACCESSIBLE_NAME_PROPERTY," +
            " Expected: " + namePropertyText +
            " Actual: " + testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY));
    }

    text = "different html text";
    testLabel.setText("<html>" + text + "</html>");
    if (!namePropertyText.equals(testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY))) {
        throw new RuntimeException("Incorrect ACCESSIBLE_NAME_PROPERTY," +
            " Expected: " + namePropertyText +
            " Actual: " + testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY));
    }
}
 
Example 2
Source Project: netbeans   File: QueryTableCellRenderer.java    License: Apache License 2.0 6 votes vote down vote up
public static void processText(JLabel label) {
    MessageFormat format = (MessageFormat) label.getClientProperty(PROPERTY_FORMAT);     // NOI18N
    Pattern pattern = (Pattern) label.getClientProperty(PROPERTY_HIGHLIGHT_PATTERN);     // NOI18N
    String s = computeFitText(label);
    if(format != null || pattern != null) {
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");                                                // NOI18N
        s = TextUtils.escapeForHTMLLabel(s);
        if(format != null) {
            format.format(new Object[] {s}, sb, null);
        }
        if(pattern != null) {
            sb.append(highLight(s, pattern));
        }
        sb.append("</html>");                                               // NOI18N
        s = sb.toString();
    } 
    label.setText(s);
}
 
Example 3
private static void testCustomLAF(boolean useAAHints) throws Exception {
    CustomLookAndFeel customLAF = new CustomLookAndFeel(useAAHints);
    UIManager.setLookAndFeel(customLAF);

    JLabel label = new JLabel();
    Object aaHint = label.getClientProperty(KEY_TEXT_ANTIALIASING);
    Object lcdContrastHint = label.getClientProperty(KEY_TEXT_LCD_CONTRAST);

    if (aaHint != customLAF.getAAHint()) {
        throw new RuntimeException("AA hint from custom L&F is not set");
    }

    if (lcdContrastHint != customLAF.getLCDContarstHint()) {
        throw new RuntimeException("AA hint from custom L&F is not set");
    }
}
 
Example 4
Source Project: pumpernickel   File: BreadCrumbUI.java    License: MIT License 6 votes vote down vote up
/**
 * Return the labels that exactly correspond to <code>jbc.getPath()</code>
 * in the analogous order.
 */
protected static JLabel[] getCrumbs(JBreadCrumb<?> jbc) {
	TreeMap<Integer, JLabel> map = new TreeMap<Integer, JLabel>();
	for (int a = 0; a < jbc.getComponentCount(); a++) {
		Component c = jbc.getComponent(a);
		if (c instanceof JLabel) {
			JLabel label = (JLabel) c;
			Integer i = (Integer) label
					.getClientProperty(PATH_NODE_INDEX_KEY);
			if (i != null) {
				map.put(i, label);
			}
		}
	}
	JLabel[] array = new JLabel[map.size()];
	int ctr = 0;
	for (Integer key : map.keySet()) {
		array[ctr++] = map.get(key);
	}
	return array;
}
 
Example 5
Source Project: pumpernickel   File: BreadCrumbUI.java    License: MIT License 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
	JBreadCrumb jbc = (JBreadCrumb) e.getComponent();
	JLabel label = getLabel(jbc, e.getPoint());
	if (label == null)
		return;

	List<NavigationListener<T>> listeners = jbc
			.getNavigationListeners();
	for (int a = 0; a < listeners.size(); a++) {
		NavigationListener<T> listener = listeners.get(a);
		ListSelectionType type = e.getClickCount() == 2 ? ListSelectionType.DOUBLE_CLICK
				: ListSelectionType.SINGLE_CLICK;
		T element = (T) label.getClientProperty(PATH_NODE_KEY);

		// stupid ClassCastException and varargs require a little fancy
		// footwork here:
		T[] array = (T[]) Array.newInstance(element.getClass(), 1);
		array[0] = element;

		listener.elementsSelected(type, array);
	}

}
 
Example 6
Source Project: pumpernickel   File: ThumbnailLabelUI.java    License: MIT License 5 votes vote down vote up
@Override
public void paint(Graphics g0, JComponent c) {
	Graphics2D g = (Graphics2D) g0;
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);

	if (c.isOpaque()) {
		g.setColor(c.getBackground());
		g.fillRect(0, 0, c.getWidth(), c.getHeight());
	}

	JLabel label = (JLabel) c;

	Object selectedValue = label.getClientProperty("selected");
	if (selectedValue == null)
		selectedValue = Boolean.FALSE;
	boolean isSelected = selectedValue.toString().equals("true");

	Object indicatedValue = label.getClientProperty("selected");
	if (indicatedValue == null)
		indicatedValue = Boolean.FALSE;
	boolean isIndicated = indicatedValue.toString().equals("true");

	calculateGeometry(label);
	paintIcon(g, label, isSelected, isIndicated);

	g.setColor(label.getForeground());
	paintText(g, label, label.getText(), isSelected, isIndicated);
}
 
Example 7
Source Project: pumpernickel   File: BreadCrumbUI.java    License: MIT License 5 votes vote down vote up
/**
 * Return the crumb at a given location.
 * 
 * @param comp
 *            the JBreadCrumb
 * @param p
 *            a point relative to the JBreadCrumb
 * @return the crumb at the given point, or null.
 */
public <E> E getCrumb(JBreadCrumb<E> comp, Point p) {
	for (int a = 0; a < comp.getComponentCount(); a++) {
		Component c = comp.getComponent(a);
		if (c.getBounds().contains(p) && c instanceof JLabel
				&& ((JLabel) c).getClientProperty(PATH_NODE_KEY) != null) {
			JLabel l = (JLabel) c;
			return (E) l.getClientProperty(PATH_NODE_KEY);
		}
	}
	return null;
}
 
Example 8
Source Project: seaglass   File: SeaGlassLabelUI.java    License: Apache License 2.0 5 votes vote down vote up
public int getBaseline(JComponent c, int width, int height) {
    if (c == null) {
        throw new NullPointerException("Component must be non-null");
    }
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Width and height must be >= 0");
    }
    JLabel label = (JLabel) c;
    String text = label.getText();
    if (text == null || "".equals(text)) {
        return -1;
    }
    Insets i = label.getInsets();
    Rectangle viewRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Rectangle iconRect = new Rectangle();
    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = width - (i.right + viewRect.x);
    viewRect.height = height - (i.bottom + viewRect.y);

    // layout the text and icon
    SeaGlassContext context = getContext(label);
    FontMetrics fm = context.getComponent().getFontMetrics(context.getStyle().getFont(context));
    context.getStyle().getGraphicsUtils(context).layoutText(context, fm, label.getText(), label.getIcon(),
        label.getHorizontalAlignment(), label.getVerticalAlignment(), label.getHorizontalTextPosition(),
        label.getVerticalTextPosition(), viewRect, iconRect, textRect, label.getIconTextGap());
    View view = (View) label.getClientProperty(BasicHTML.propertyKey);
    int baseline;
    if (view != null) {
        baseline = BasicHTML.getHTMLBaseline(view, textRect.width, textRect.height);
        if (baseline >= 0) {
            baseline += textRect.y;
        }
    } else {
        baseline = textRect.y + fm.getAscent();
    }
    context.dispose();
    return baseline;
}
 
Example 9
/**
 * Resizes the {@code View} object used by this label to match
 * the current size of the label. Resizing of the {@code View}
 * causes re-layout of HTML label (which affects its preferred size).
 * 
 * @param label label whose {@code View} should be resized.
 */
private void resizeViewToMatchTheCurrentSize(JLabel label) {
    Object view = label.getClientProperty("html"); // NOI18N
    if (view instanceof View) {
        ((View)view).setSize(label.getWidth(), label.getHeight());
    }
}
 
Example 10
public static java.awt.Dimension getPreferredSize(String text,
                                                  boolean width, int prefSize) {

    JLabel resizer = new JLabel(text);

    View view = (View) resizer
            .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);

    view.setSize(width ? prefSize : 0, width ? 0 : prefSize);

    float w = view.getPreferredSpan(View.X_AXIS);
    float h = view.getPreferredSpan(View.Y_AXIS) + 2;

    return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}