2
Copyright 2008-2011 Gephi
3
Authors : Mathieu Bastian
4
Website : http://www.gephi.org
6
This file is part of Gephi.
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.
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.
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/>.
21
package org.gephi.desktop.preview.propertyeditors;
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;
32
* @author Mathieu Bastian
34
public class EdgeColorPropertyEditor extends PropertyEditorSupport {
37
public Component getCustomEditor() {
38
EdgeColorPanel edgeColorPanel = new EdgeColorPanel();
39
edgeColorPanel.setup(this);
40
return edgeColorPanel;
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();
50
c.getMode().name().toLowerCase(),
55
return c.getMode().name().toLowerCase();
60
public void setAsText(String s) {
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);
66
int r = Integer.valueOf(m.group(1));
67
int g = Integer.valueOf(m.group(2));
68
int b = Integer.valueOf(m.group(3));
70
setValue(new EdgeColor(new Color(r, g, b)));
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));
84
public boolean supportsCustomEditor() {
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);