Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit 29802be

Browse files
refactor: replace legend/cluster hardcoding with EdgeType-driven logic
- initializeLegendSpace(): replace 50 lines of duplicated icon/label creation with a 5-line loop over EdgeType.values() - createLayout(): replace 30-line if/else areaId chain with EdgeType.clusterIdFor() using a static bitmask lookup map - Add legendIconPath field and clusterIdFor() to EdgeType enum - Remove unused isSg variable from vertex clustering Reduces Main.java by ~60 lines and makes adding new edge types a single-line enum entry instead of editing 3+ methods.
1 parent 1b6a50e commit 29802be

2 files changed

Lines changed: 77 additions & 86 deletions

File tree

Gvisual/src/gvisual/EdgeType.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,74 @@
1818
*/
1919
public enum EdgeType {
2020

21-
FRIEND ("f", "friend", Color.GREEN, 10, 2),
22-
CLASSMATE ("c", "Classmate", Color.BLUE, 30, 1),
23-
FAMILIAR ("fs", "Familiar Stranger", Color.GRAY, 2, 1),
24-
STRANGER ("s", "Stranger", Color.RED, 2, 2),
25-
STUDY_GROUP ("sg", "Study Groups", Color.ORANGE, 20, 1);
21+
FRIEND ("f", "friend", Color.GREEN, "images/green.jpg", 10, 2),
22+
CLASSMATE ("c", "Classmate", Color.BLUE, "images/blue.jpg", 30, 1),
23+
FAMILIAR ("fs", "Familiar Stranger", Color.GRAY, "images/gray.jpg", 2, 1),
24+
STRANGER ("s", "Stranger", Color.RED, "images/red.jpg", 2, 2),
25+
STUDY_GROUP ("sg", "Study Groups", Color.ORANGE,"images/yellow.jpg", 20, 1);
2626

2727
private final String code;
2828
private final String displayLabel;
2929
private final Color color;
30+
private final String legendIconPath;
3031
private final int defaultDurationThreshold;
3132
private final int defaultMeetingThreshold;
3233

33-
EdgeType(String code, String displayLabel, Color color,
34+
/**
35+
* Bit-flag for cluster layout assignment. FRIEND=1, FAMILIAR=2,
36+
* CLASSMATE=4, STRANGER=8. STUDY_GROUP is excluded from clustering.
37+
*/
38+
private static final int FLAG_FRIEND = 1;
39+
private static final int FLAG_FAMILIAR = 2;
40+
private static final int FLAG_CLASSMATE = 4;
41+
private static final int FLAG_STRANGER = 8;
42+
43+
/**
44+
* Maps a combination of edge-type presence flags to a cluster area ID
45+
* (0..8) used by {@code Main.createLayout()}.
46+
*
47+
* <p>The flags are: FRIEND=1, FAMILIAR=2, CLASSMATE=4, STRANGER=8.
48+
* Only the 9 original cluster assignments are mapped; all other
49+
* combinations fall into the catch-all cluster 4.</p>
50+
*/
51+
private static final java.util.Map<Integer, Integer> CLUSTER_MAP;
52+
static {
53+
CLUSTER_MAP = new java.util.HashMap<>();
54+
CLUSTER_MAP.put(FLAG_FRIEND, 0); // F only
55+
CLUSTER_MAP.put(FLAG_FRIEND | FLAG_CLASSMATE, 1); // F + C
56+
CLUSTER_MAP.put(FLAG_CLASSMATE, 2); // C only
57+
CLUSTER_MAP.put(FLAG_FRIEND | FLAG_FAMILIAR, 3); // F + FS
58+
// 4 = catch-all (handled by getOrDefault below)
59+
CLUSTER_MAP.put(FLAG_CLASSMATE | FLAG_STRANGER, 5); // C + S
60+
CLUSTER_MAP.put(FLAG_FAMILIAR, 6); // FS only
61+
CLUSTER_MAP.put(FLAG_FAMILIAR | FLAG_STRANGER, 7); // FS + S
62+
CLUSTER_MAP.put(FLAG_STRANGER, 8); // S only
63+
}
64+
65+
/**
66+
* Returns the cluster area ID (0..8) for a set of edge-type presence
67+
* flags. Replaces the 30-line if/else chain in {@code Main.createLayout()}.
68+
*
69+
* @param isF vertex has friend edges
70+
* @param isFs vertex has familiar-stranger edges
71+
* @param isC vertex has classmate edges
72+
* @param isS vertex has stranger edges
73+
* @return cluster ID for layout positioning
74+
*/
75+
public static int clusterIdFor(boolean isF, boolean isFs, boolean isC, boolean isS) {
76+
int flags = (isF ? FLAG_FRIEND : 0)
77+
| (isFs ? FLAG_FAMILIAR : 0)
78+
| (isC ? FLAG_CLASSMATE : 0)
79+
| (isS ? FLAG_STRANGER : 0);
80+
return CLUSTER_MAP.getOrDefault(flags, 4);
81+
}
82+
83+
EdgeType(String code, String displayLabel, Color color, String legendIconPath,
3484
int defaultDurationThreshold, int defaultMeetingThreshold) {
3585
this.code = code;
3686
this.displayLabel = displayLabel;
3787
this.color = color;
88+
this.legendIconPath = legendIconPath;
3889
this.defaultDurationThreshold = defaultDurationThreshold;
3990
this.defaultMeetingThreshold = defaultMeetingThreshold;
4091
}
@@ -49,6 +100,11 @@ public String getDisplayLabel() {
49100
return displayLabel;
50101
}
51102

103+
/** Returns the legend icon image path (e.g. "images/green.jpg"). */
104+
public String getLegendIconPath() {
105+
return legendIconPath;
106+
}
107+
52108
/** Returns the default Edge colour for rendering. */
53109
public Color getColor() {
54110
return color;

Gvisual/src/gvisual/Main.java

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ public void createLayout() {
274274
boolean isFs = false;
275275
boolean isC = false;
276276
boolean isS = false;
277-
boolean isSg = false;
278-
int areaId;
279277
for (Edge y : g.getOutEdges(x)) {
280278
EdgeType type = EdgeType.fromCode(y.getType());
281279
if (type != null) {
@@ -284,31 +282,12 @@ public void createLayout() {
284282
case FAMILIAR: isFs = true; break;
285283
case CLASSMATE: isC = true; break;
286284
case STRANGER: isS = true; break;
287-
case STUDY_GROUP: isSg = true; break;
285+
case STUDY_GROUP: break; // excluded from clustering
288286
}
289287
}
290288
}
291-
// To be added study groups
292-
if (isF && !isFs && !isC && !isS) {
293-
areaId = 0;
294-
} else if (isF && isFs && !isC && !isS) {
295-
areaId = 3;
296-
} else if (isF && !isFs && isC && !isS) {
297-
areaId = 1;
298-
} else if (!isF && !isFs && isC && !isS) {
299-
areaId = 2;
300-
} else if (!isF && !isFs && isC && isS) {
301-
areaId = 5;
302-
} else if (!isF && isFs && !isC && isS) {
303-
areaId = 7;
304-
} else if (!isF && isFs && !isC && !isS) {
305-
areaId = 6;
306-
} else if (!isF && !isFs && !isC && isS) {
307-
areaId = 8;
308-
} else {
309-
areaId = 4;
310-
}
311289

290+
int areaId = EdgeType.clusterIdFor(isF, isFs, isC, isS);
312291
clusters.get(areaId).add(x);
313292
}
314293

@@ -632,70 +611,26 @@ public void addGraph() throws ParserConfigurationException, IOException, SAXExce
632611
/**
633612
* initialize the Legend Space
634613
*/
635-
public final void initializeLegendSpace(){
614+
/**
615+
* Builds the legend panel by iterating over {@link EdgeType} values,
616+
* replacing the previous 50-line block of duplicated icon/label code.
617+
*/
618+
public final void initializeLegendSpace() {
636619
legendPanel = new JPanel();
637620

638621
JLabel legendHeading = new JLabel("Legend for the Graph");
622+
Box legendBox = Box.createVerticalBox();
639623

640-
String blueImgPath = "images/blue.jpg";
641-
String redImgPath = "images/red.jpg";
642-
String greenImgPath = "images/green.jpg";
643-
String yellowImgPath = "images/yellow.jpg";
644-
String grayImgPath = "images/gray.jpg";
645-
646-
Icon blue = new ImageIcon(blueImgPath);
647-
Icon red = new ImageIcon(redImgPath);
648-
Icon green = new ImageIcon(greenImgPath);
649-
Icon yellow = new ImageIcon(yellowImgPath);
650-
Icon gray = new ImageIcon(grayImgPath);
651-
652-
JLabel fLabel = new JLabel("Friend");
653-
JLabel fColor= new JLabel(green);
654-
JLabel fsLabel= new JLabel("Familiar Stranger");
655-
JLabel fsColor= new JLabel(gray);
656-
JLabel sLabel= new JLabel("Stranger");
657-
JLabel sColor= new JLabel(red);
658-
JLabel cLabel= new JLabel("Classmate");
659-
JLabel cColor = new JLabel(blue);
660-
JLabel sgLabel= new JLabel("Study Group");
661-
JLabel sgColor= new JLabel(yellow);
662-
663-
Box HBox[] = new Box[5];
664-
Box VBox[] = new Box[1];
665-
666-
VBox[0]= Box.createVerticalBox();
667-
668-
HBox[0]= Box.createHorizontalBox();
669-
HBox[1]= Box.createHorizontalBox();
670-
HBox[2]= Box.createHorizontalBox();
671-
HBox[3]= Box.createHorizontalBox();
672-
HBox[4]= Box.createHorizontalBox();
673-
674-
675-
HBox[0].add(fColor);
676-
HBox[1].add(fsColor);
677-
HBox[2].add(sColor);
678-
HBox[3].add(cColor);
679-
HBox[4].add(sgColor);
680-
681-
HBox[0].add(fLabel);
682-
HBox[1].add(fsLabel);
683-
HBox[2].add(sLabel);
684-
HBox[3].add(cLabel);
685-
HBox[4].add(sgLabel);
686-
687-
VBox[0].add(HBox[0]);
688-
VBox[0].add(HBox[1]);
689-
VBox[0].add(HBox[2]);
690-
VBox[0].add(HBox[3]);
691-
VBox[0].add(HBox[4]);
624+
for (EdgeType type : EdgeType.values()) {
625+
Box row = Box.createHorizontalBox();
626+
row.add(new JLabel(new ImageIcon(type.getLegendIconPath())));
627+
row.add(new JLabel(type.getDisplayLabel()));
628+
legendBox.add(row);
629+
}
692630

693631
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
694-
legendHeading, VBox[0]);
695-
696-
632+
legendHeading, legendBox);
697633
legendPanel.add(splitPane);
698-
699634
}
700635

701636
/**

0 commit comments

Comments
 (0)