~duyi001/gephi/DSNI

« back to all changes in this revision

Viewing changes to DesktopPreview/src/org/gephi/desktop/preview/propertyeditors/EdgeColorPropertyEditor.java

  • Committer: sunsnowad
  • Author(s): Yi Du
  • Date: 2011-09-08 16:36:59 UTC
  • mfrom: (1435.1.968 gephi)
  • Revision ID: sunsnowad@www-691ed046717-20110908163659-aorx14ylp8f9qwdx
1.merge with main branch
2.update twitter4j to version 2.2.4
3.fix an existing bug on "twitter user import"

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2008-2011 Gephi
 
3
Authors : Mathieu Bastian
 
4
Website : http://www.gephi.org
 
5
 
 
6
This file is part of Gephi.
 
7
 
 
8
Gephi is free software: you can redistribute it and/or modify
 
9
it under the terms of the GNU Affero General Public License as
 
10
published by the Free Software Foundation, either version 3 of the
 
11
License, or (at your option) any later version.
 
12
 
 
13
Gephi is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU Affero General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU Affero General Public License
 
19
along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
package org.gephi.desktop.preview.propertyeditors;
 
22
 
 
23
import java.awt.Color;
 
24
import java.awt.Component;
 
25
import java.beans.PropertyEditorSupport;
 
26
import java.util.regex.Matcher;
 
27
import java.util.regex.Pattern;
 
28
import org.gephi.preview.types.EdgeColor;
 
29
 
 
30
/**
 
31
 *
 
32
 * @author Mathieu Bastian
 
33
 */
 
34
public class EdgeColorPropertyEditor extends PropertyEditorSupport {
 
35
 
 
36
    @Override
 
37
    public Component getCustomEditor() {
 
38
        EdgeColorPanel edgeColorPanel = new EdgeColorPanel();
 
39
        edgeColorPanel.setup(this);
 
40
        return edgeColorPanel;
 
41
    }
 
42
 
 
43
    @Override
 
44
    public String getAsText() {
 
45
        EdgeColor c = (EdgeColor) getValue();
 
46
        if (c.getMode().equals(EdgeColor.Mode.CUSTOM)) {
 
47
            Color color = c.getCustomColor() == null ? Color.BLACK : c.getCustomColor();
 
48
            return String.format(
 
49
                    "%s [%d,%d,%d]",
 
50
                    c.getMode().name().toLowerCase(),
 
51
                    color.getRed(),
 
52
                    color.getGreen(),
 
53
                    color.getBlue());
 
54
        } else {
 
55
            return c.getMode().name().toLowerCase();
 
56
        }
 
57
    }
 
58
 
 
59
    @Override
 
60
    public void setAsText(String s) {
 
61
 
 
62
        if (matchColorMode(s, EdgeColor.Mode.CUSTOM.name().toLowerCase())) {
 
63
            Pattern p = Pattern.compile("\\w+\\s*\\[\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\]");
 
64
            Matcher m = p.matcher(s);
 
65
            if (m.lookingAt()) {
 
66
                int r = Integer.valueOf(m.group(1));
 
67
                int g = Integer.valueOf(m.group(2));
 
68
                int b = Integer.valueOf(m.group(3));
 
69
 
 
70
                setValue(new EdgeColor(new Color(r, g, b)));
 
71
            }
 
72
        } else if (matchColorMode(s, EdgeColor.Mode.MIXED.name().toLowerCase())) {
 
73
            setValue(new EdgeColor(EdgeColor.Mode.MIXED));
 
74
        } else if (matchColorMode(s, EdgeColor.Mode.ORIGINAL.name().toLowerCase())) {
 
75
            setValue(new EdgeColor(EdgeColor.Mode.ORIGINAL));
 
76
        } else if (matchColorMode(s, EdgeColor.Mode.SOURCE.name().toLowerCase())) {
 
77
            setValue(new EdgeColor(EdgeColor.Mode.SOURCE));
 
78
        } else if (matchColorMode(s, EdgeColor.Mode.TARGET.name().toLowerCase())) {
 
79
            setValue(new EdgeColor(EdgeColor.Mode.TARGET));
 
80
        }
 
81
    }
 
82
 
 
83
    @Override
 
84
    public boolean supportsCustomEditor() {
 
85
        return true;
 
86
    }
 
87
 
 
88
    private boolean matchColorMode(String s, String identifier) {
 
89
        String regexp = String.format("\\s*%s\\s*", identifier);
 
90
        Pattern p = Pattern.compile(regexp);
 
91
        Matcher m = p.matcher(s);
 
92
        return m.lookingAt();
 
93
    }
 
94
}