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 package net.sf.bluecove.obex.server;
26
27 import java.awt.BorderLayout;
28 import java.awt.Dimension;
29 import java.awt.GridBagConstraints;
30 import java.awt.GridBagLayout;
31 import java.awt.Image;
32 import java.awt.Toolkit;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35
36 import javax.bluetooth.LocalDevice;
37 import javax.swing.BorderFactory;
38 import javax.swing.Box;
39 import javax.swing.BoxLayout;
40 import javax.swing.ImageIcon;
41 import javax.swing.JButton;
42 import javax.swing.JFrame;
43 import javax.swing.JLabel;
44 import javax.swing.JPanel;
45 import javax.swing.JProgressBar;
46 import javax.swing.SwingUtilities;
47 import javax.swing.border.EmptyBorder;
48
49
50
51
52 public class Main extends JFrame implements ActionListener, UserInteraction {
53
54 private static final long serialVersionUID = 1L;
55
56 private JLabel iconLabel;
57
58 private String status;
59
60 JProgressBar progressBar;
61
62 private ImageIcon btIcon;
63
64 private ImageIcon transferIcon;
65
66 private JButton btExit;
67
68 private OBEXServer server;
69
70 private static void createAndShowGUI(final String[] args) {
71 final Main app = new Main();
72 app.pack();
73 app.center();
74 app.setVisible(true);
75 SwingUtilities.invokeLater(new Runnable() {
76 public void run() {
77 app.initializeServer();
78 }
79 });
80 }
81
82 public static void main(final String[] args) {
83 SwingUtilities.invokeLater(new Runnable() {
84 public void run() {
85 createAndShowGUI(args);
86 }
87 });
88 }
89
90 private void center() {
91 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
92 this.setLocation(((screenSize.width - this.getWidth()) / 2), ((screenSize.height - this.getHeight()) / 2));
93 }
94
95 private Main() {
96 super("BlueCove OBEX Server");
97 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
98
99 Image btImage = Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/icon.png"));
100 btIcon = new ImageIcon(btImage);
101 transferIcon = new ImageIcon((Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/transfer.png"))));
102
103 this.setIconImage(btImage);
104
105 JPanel contentPane = (JPanel) this.getContentPane();
106 contentPane.setLayout(new BorderLayout(10, 10));
107 contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
108
109 JPanel progressPanel = new JPanel();
110 progressPanel.setLayout(new GridBagLayout());
111 GridBagConstraints c = new GridBagConstraints();
112
113 iconLabel = new JLabel();
114 iconLabel.setIcon(btIcon);
115 c.fill = GridBagConstraints.BOTH;
116 c.weightx = 1.0;
117 progressPanel.add(iconLabel, c);
118
119 progressBar = new JProgressBar(0, 100);
120 progressBar.setValue(0);
121 progressBar.setStringPainted(true);
122
123 c.fill = GridBagConstraints.HORIZONTAL;
124 c.gridwidth = GridBagConstraints.REMAINDER;
125 progressPanel.add(progressBar, c);
126
127 getContentPane().add(progressPanel, BorderLayout.NORTH);
128
129 JPanel actionPanel = new JPanel();
130 actionPanel.setLayout(new BoxLayout(actionPanel, BoxLayout.LINE_AXIS));
131 actionPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
132 actionPanel.add(Box.createHorizontalGlue());
133
134 actionPanel.add(btExit = new JButton("Exit"));
135 btExit.addActionListener(this);
136
137 contentPane.add(actionPanel, BorderLayout.SOUTH);
138
139 }
140
141
142
143
144
145
146 public void actionPerformed(ActionEvent e) {
147 if (e.getSource() == btExit) {
148 shutdown();
149 System.exit(0);
150 }
151 }
152
153 public void showStatus(final String message) {
154 status = message;
155 progressBar.setString(message);
156 }
157
158 public void setProgressMaximum(int n) {
159 progressBar.setMaximum(n);
160 }
161
162 public void setProgressValue(int n) {
163 progressBar.setValue(n);
164 SwingUtilities.invokeLater(new Runnable() {
165 public void run() {
166 progressBar.setString(status);
167 }
168 });
169 }
170
171 public void setProgressDone() {
172 progressBar.setValue(0);
173 }
174
175 protected void disabledBluetooth() {
176 showStatus("BlueCove not avalable");
177 iconLabel.setIcon(new ImageIcon((Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/bt-off.png")))));
178 }
179
180 private boolean initializeServer() {
181 try {
182 LocalDevice localDevice = LocalDevice.getLocalDevice();
183 if ("000000000000".equals(localDevice.getBluetoothAddress())) {
184 throw new Exception();
185 }
186 server = OBEXServer.startServer(this);
187 showStatus("BlueCove Ready");
188 return true;
189 } catch (Throwable e) {
190 Logger.debug(e);
191 disabledBluetooth();
192 return false;
193 }
194 }
195
196 private void shutdown() {
197 if (server != null) {
198 server.close();
199 }
200 }
201 }