-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCPopupMenu.java
More file actions
102 lines (94 loc) · 3.11 KB
/
Copy pathCPopupMenu.java
File metadata and controls
102 lines (94 loc) · 3.11 KB
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
100
101
102
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import openblocks.codeblockutil.CScrollPane.ScrollPolicy;
public class CPopupMenu extends JPopupMenu implements ActionListener{
private static final long serialVersionUID = 328149080311L;
private int ITEM_HEIGHT = 14;
private Color background = CGraphite.white;
private JComponent scroll;
private JComponent view;
private double zoom = 1.0;
private int items=0;
//private int MARGIN = 20;
public CPopupMenu(){
super();
this.setLayout(new BorderLayout());
this.setBackground(background);
this.setOpaque(false);
this.removeAll();
this.setBorder(BorderFactory.createMatteBorder(2,2,2,2, CGraphite.darkgreen));
view = new JPanel(new GridLayout(0,1));
view.setBackground(background);
scroll = new CTracklessScrollPane(view,
ScrollPolicy.VERTICAL_BAR_AS_NEEDED,
ScrollPolicy.HORIZONTAL_BAR_NEVER,
9,CGraphite.darkgreen, background);
this.add(scroll);
}
public Insets getInsets(){
return new Insets(5,5,5,5);
}
public void add(CMenuItem item){
items++;
item.addActionListener(this);
view.add(item);
}
public void setZoomLevel(double zoom){
this.zoom = zoom;
// change the size of the panel and the text
view.setPreferredSize(new Dimension((int) (100*this.zoom), (int) (items*ITEM_HEIGHT*this.zoom)));
this.setPopupSize((int)(100*this.zoom), (int)(Math.min(items*ITEM_HEIGHT+10, 100)*this.zoom));
}
public void actionPerformed(ActionEvent e) {
if (this.isVisible()) {
this.setVisible(false);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.setSize(500, 300);
f.getContentPane().setBackground(Color.red);
final CButton c = new CGraphiteButton("hi");
f.add(c);
f.setVisible(true);
f.repaint();
final CPopupMenu menu = new CPopupMenu();
for(int i = 0; i<17; i++){
menu.add(new CMenuItem("hi"));
}
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
menu.show(c, 0, 0);
}
});
c.add(menu);
System.out.println(menu.getParent());
}
/* protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(background);
g.fillRoundRect(0,0,this.getWidth()-1, this.getHeight()-1, MARGIN, MARGIN);
g.setColor(CGraphite.milky);
g.drawRoundRect(0,0,this.getWidth()-1, this.getHeight()-1, MARGIN, MARGIN);
}*/
}