Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3060406fd995daa250e1a7feb5065b4528831c6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2007, 2009 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.ui.viewmodel.properties;

import com.ibm.icu.text.MessageFormat;
import java.util.Map;

import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;

/**
 * The text attribute of a label.  It uses a message format string in order to 
 * compose the text string.  The parameter names determine the array of objects
 * given to the message format. 
 * 
 * @see MessageFormat#format(Object[], StringBuffer, java.text.FieldPosition)
 * @see LabelAttribute
 * @see LabelColumnInfo
 * @see PropertiesBasedLabelProvider
 * 
 * @since 1.0
 */
public class LabelText extends LabelAttribute {
    
    public static final MessageFormat DEFAULT_MESSAGE = new MessageFormat(MessagesForProperties.DefaultLabelMessage_label);
   
    /**
     * Message format used to generate the label text.
     * 
     */
    private MessageFormat fMessageFormat;
    
    /**
     * The property names needed for the message format.  The property values 
     * corresponding to these names are given the the {@link MessageFormat#format(Object[], StringBuffer, java.text.FieldPosition)}
     * method.  
     */
    private String[] fPropertyNames;

    public LabelText() {
        this(DEFAULT_MESSAGE, EMPTY_PROPERTY_NAMES_ARRAY);
    }

    /**
     * @since 2.0
     * @param formatPattern
     * @param propertyNames
     */
    public LabelText(String formatPattern, String[] propertyNames) {
        this (new MessageFormat(formatPattern), propertyNames);
    }

    public LabelText(MessageFormat format, String[] propertyNames) {
        fMessageFormat = format;
        fPropertyNames = propertyNames;
    }
    
    @Override
    public String[] getPropertyNames() {
        return fPropertyNames;
    }
    
    public MessageFormat getMessageFormat() {
        return fMessageFormat;
    }

    public void setMessageFormat(MessageFormat messageFormat) {
        fMessageFormat = messageFormat;
    }

    @Override
    public void updateAttribute(ILabelUpdate update, int columnIndex, IStatus status, Map<String, Object> properties) {
        String[] propertyNames = getPropertyNames();
        Object[] propertyValues = new Object[propertyNames.length];
        for (int i = 0; i < propertyNames.length; i++) {
            propertyValues[i] = getPropertyValue(propertyNames[i], status, properties); 
        }
        
        try {
            update.setLabel(getMessageFormat().format(propertyValues, new StringBuffer(), null).toString(), columnIndex);
        } catch (IllegalArgumentException e) {
            update.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, 0, "Failed formatting a message for column " + columnIndex + ", for update " + update, e)); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }
    
    protected Object getPropertyValue(String propertyName, IStatus status, Map<String, Object> properties) {
        return properties.get(propertyName);
    }
}

Back to the top