PrinterDialog.java 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  1. /* PrinterDialog.java --
  2. Copyright (C) 2006, 2010 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.print;
  32. import java.awt.BorderLayout;
  33. import java.awt.Dimension;
  34. import java.awt.Frame;
  35. import java.awt.GraphicsConfiguration;
  36. import java.awt.GridBagConstraints;
  37. import java.awt.GridBagLayout;
  38. import java.awt.HeadlessException;
  39. import java.awt.Insets;
  40. import java.awt.event.ActionEvent;
  41. import java.awt.event.ActionListener;
  42. import java.awt.event.FocusEvent;
  43. import java.awt.event.FocusListener;
  44. import java.util.ArrayList;
  45. import java.util.ResourceBundle;
  46. import javax.print.DocFlavor;
  47. import javax.print.PrintService;
  48. import javax.print.attribute.Attribute;
  49. import javax.print.attribute.HashPrintRequestAttributeSet;
  50. import javax.print.attribute.PrintRequestAttributeSet;
  51. import javax.print.attribute.standard.Chromaticity;
  52. import javax.print.attribute.standard.Copies;
  53. import javax.print.attribute.standard.Destination;
  54. import javax.print.attribute.standard.JobName;
  55. import javax.print.attribute.standard.JobPriority;
  56. import javax.print.attribute.standard.JobSheets;
  57. import javax.print.attribute.standard.Media;
  58. import javax.print.attribute.standard.MediaPrintableArea;
  59. import javax.print.attribute.standard.OrientationRequested;
  60. import javax.print.attribute.standard.PageRanges;
  61. import javax.print.attribute.standard.PrintQuality;
  62. import javax.print.attribute.standard.PrinterInfo;
  63. import javax.print.attribute.standard.PrinterIsAcceptingJobs;
  64. import javax.print.attribute.standard.PrinterMakeAndModel;
  65. import javax.print.attribute.standard.PrinterState;
  66. import javax.print.attribute.standard.RequestingUserName;
  67. import javax.print.attribute.standard.SheetCollate;
  68. import javax.print.attribute.standard.Sides;
  69. import javax.swing.BorderFactory;
  70. import javax.swing.Box;
  71. import javax.swing.BoxLayout;
  72. import javax.swing.ButtonGroup;
  73. import javax.swing.JButton;
  74. import javax.swing.JCheckBox;
  75. import javax.swing.JComboBox;
  76. import javax.swing.JDialog;
  77. import javax.swing.JLabel;
  78. import javax.swing.JPanel;
  79. import javax.swing.JRadioButton;
  80. import javax.swing.JSpinner;
  81. import javax.swing.JTabbedPane;
  82. import javax.swing.JTextField;
  83. import javax.swing.SpinnerNumberModel;
  84. import javax.swing.border.TitledBorder;
  85. import javax.swing.event.ChangeEvent;
  86. import javax.swing.event.ChangeListener;
  87. /**
  88. * Implementation of the PrinterDialog used by
  89. * {@link javax.print.ServiceUI} for visual selection
  90. * of print services and its attributes.
  91. *
  92. * @author Wolfgang Baer (WBaer@gmx.de)
  93. */
  94. public final class PrinterDialog extends JDialog implements ActionListener
  95. {
  96. /**
  97. * The General Panel used in the printing dialog.
  98. * @author Wolfgang Baer (WBaer@gmx.de)
  99. */
  100. final class GeneralPanel extends JPanel
  101. {
  102. /**
  103. * Handles the copies attribute.
  104. * @author Wolfgang Baer (WBaer@gmx.de)
  105. */
  106. final class CopiesAndSorted extends JPanel
  107. implements ChangeListener, ActionListener
  108. {
  109. private JCheckBox sort;
  110. private JSpinner copies;
  111. private JLabel copies_lb;
  112. private SpinnerNumberModel copiesModel;
  113. CopiesAndSorted()
  114. {
  115. copies_lb = new JLabel(getLocalizedString("lb.copies"));
  116. sort = new JCheckBox(getLocalizedString("cb.sort"));
  117. sort.addActionListener(this);
  118. copiesModel = new SpinnerNumberModel(1, 1, 9999, 1);
  119. copies = new JSpinner(copiesModel);
  120. copies.addChangeListener(this);
  121. GridBagLayout layout = new GridBagLayout();
  122. GridBagConstraints c = new GridBagConstraints();
  123. c.fill = GridBagConstraints.BOTH;
  124. c.insets = new Insets(5, 5, 5, 5);
  125. setLayout(layout);
  126. setBorder(new TitledBorder(getLocalizedString("title.copies")));
  127. c.anchor = GridBagConstraints.WEST;
  128. c.gridx = 0;
  129. c.gridy = 0;
  130. add(copies_lb, c);
  131. c.gridx = 1;
  132. c.gridy = 0;
  133. add(copies, c);
  134. c.gridx = 0;
  135. c.gridy = 1;
  136. add(sort, c);
  137. }
  138. // copies jspinner state
  139. public void stateChanged(ChangeEvent event)
  140. {
  141. int value = ((Integer) copies.getValue()).intValue();
  142. atts.add(new Copies(value));
  143. if (value > 1 && categorySupported(SheetCollate.class))
  144. sort.setEnabled(true);
  145. else
  146. sort.setEnabled(false);
  147. }
  148. // sorted checkbox state
  149. public void actionPerformed(ActionEvent event)
  150. {
  151. if (sort.isSelected())
  152. atts.add(SheetCollate.COLLATED);
  153. }
  154. /**
  155. * Called to update for new selected
  156. * print service. Tests if currently
  157. * selected attributes are supported.
  158. */
  159. void updateForSelectedService()
  160. {
  161. if (categorySupported(Copies.class))
  162. {
  163. copies.setEnabled(true);
  164. copies_lb.setEnabled(true);
  165. Copies copies = (Copies) attribute(Copies.class);
  166. if (copies != null)
  167. copiesModel.setValue(new Integer(copies.getValue()));
  168. if (((Integer)copiesModel.getValue()).intValue() > 1
  169. && categorySupported(SheetCollate.class))
  170. {
  171. sort.setEnabled(true);
  172. Attribute collate = attribute(SheetCollate.class);
  173. if (collate != null && collate.equals(SheetCollate.COLLATED))
  174. sort.setSelected(true);
  175. }
  176. else
  177. sort.setEnabled(false);
  178. }
  179. else
  180. {
  181. copies.setEnabled(false);
  182. copies_lb.setEnabled(false);
  183. }
  184. }
  185. }
  186. /**
  187. * Handles the print ranges attribute.
  188. * @author Wolfgang Baer (WBaer@gmx.de)
  189. */
  190. final class PrintRange extends JPanel
  191. implements ActionListener, FocusListener
  192. {
  193. private JLabel to;
  194. private JRadioButton all_rb, pages_rb;
  195. private JTextField from_tf, to_tf;
  196. PrintRange()
  197. {
  198. to = new JLabel(getLocalizedString("lb.to"));
  199. to.setEnabled(false);
  200. all_rb = new JRadioButton(getLocalizedString("rbt.all"));
  201. all_rb.setSelected(true);
  202. all_rb.setActionCommand("ALL");
  203. all_rb.addActionListener(this);
  204. pages_rb = new JRadioButton(getLocalizedString("rbt.pages"));
  205. pages_rb.setActionCommand("PAGES");
  206. pages_rb.setEnabled(false);
  207. pages_rb.addActionListener(this);
  208. ButtonGroup group = new ButtonGroup();
  209. group.add(all_rb);
  210. group.add(pages_rb);
  211. from_tf = new JTextField("1", 4);
  212. from_tf.setEnabled(false);
  213. from_tf.addFocusListener(this);
  214. to_tf = new JTextField("1", 4);
  215. to_tf.setEnabled(false);
  216. to_tf.addFocusListener(this);
  217. GridBagLayout layout = new GridBagLayout();
  218. GridBagConstraints c = new GridBagConstraints();
  219. c.fill = GridBagConstraints.BOTH;
  220. setLayout(layout);
  221. setBorder(new TitledBorder(getLocalizedString("title.printrange")));
  222. c.insets = new Insets(15, 5, 5, 5);
  223. c.gridx = 0;
  224. c.gridy = 0;
  225. add(all_rb, c);
  226. c.insets = new Insets(5, 5, 15, 5);
  227. c.gridx = 0;
  228. c.gridy = 1;
  229. add(pages_rb, c);
  230. c.gridx = 1;
  231. c.gridy = 1;
  232. add(from_tf, c);
  233. c.gridx = 2;
  234. c.gridy = 1;
  235. add(to, c);
  236. c.insets = new Insets(5, 5, 15, 15);
  237. c.gridx = 3;
  238. c.gridy = 1;
  239. add(to_tf, c);
  240. }
  241. // focus pagerange
  242. public void focusGained(FocusEvent event)
  243. {
  244. updatePageRanges();
  245. }
  246. public void focusLost(FocusEvent event)
  247. {
  248. updatePageRanges();
  249. }
  250. // updates the range after user changed it
  251. private void updatePageRanges()
  252. {
  253. int lower = Integer.parseInt(from_tf.getText());
  254. int upper = Integer.parseInt(to_tf.getText());
  255. if (lower > upper)
  256. {
  257. upper = lower;
  258. to_tf.setText("" + lower);
  259. }
  260. PageRanges range = new PageRanges(lower, upper);
  261. atts.add(range);
  262. }
  263. // page range change
  264. public void actionPerformed(ActionEvent e)
  265. {
  266. // if ALL is selected we must use a full-range object
  267. if (e.getActionCommand().equals("ALL"))
  268. {
  269. from_tf.setEnabled(false);
  270. to.setEnabled(false);
  271. to_tf.setEnabled(false);
  272. atts.add(new PageRanges(1, Integer.MAX_VALUE));
  273. }
  274. else
  275. {
  276. from_tf.setEnabled(true);
  277. to.setEnabled(true);
  278. to_tf.setEnabled(true);
  279. all_rb.setSelected(false);
  280. }
  281. }
  282. /**
  283. * Called to update for new selected
  284. * print service. Tests if currently
  285. * selected attributes are supported.
  286. */
  287. void updateForSelectedService()
  288. {
  289. if (categorySupported(PageRanges.class))
  290. {
  291. pages_rb.setEnabled(true);
  292. PageRanges range = (PageRanges) attribute(PageRanges.class);
  293. if (range != null)
  294. {
  295. from_tf.setEnabled(true);
  296. to.setEnabled(true);
  297. to_tf.setEnabled(true);
  298. all_rb.setSelected(false);
  299. pages_rb.setSelected(true);
  300. int[][] members = range.getMembers();
  301. // Although passed in attributes may contain more than one
  302. // range we only take the first one
  303. from_tf.setText("" + members[0][0]);
  304. to_tf.setText("" + members[0][1]);
  305. }
  306. }
  307. else
  308. {
  309. from_tf.setEnabled(false);
  310. to.setEnabled(false);
  311. to_tf.setEnabled(false);
  312. all_rb.setSelected(true);
  313. }
  314. }
  315. }
  316. /**
  317. * Handles the selection of the print services
  318. * and its location and description attributes.
  319. * @author Wolfgang Baer (WBaer@gmx.de)
  320. */
  321. final class PrintServices extends JPanel
  322. implements ActionListener
  323. {
  324. private JLabel name, status, typ, info;
  325. private JLabel statusValue, typValue, infoValue;
  326. private JButton attributes;
  327. private JComboBox services_cob;
  328. private JCheckBox fileRedirection_cb;
  329. PrintServices()
  330. {
  331. name = new JLabel(getLocalizedString("lb.name"));
  332. status = new JLabel(getLocalizedString("lb.status"));
  333. typ = new JLabel(getLocalizedString("lb.typ"));
  334. info = new JLabel(getLocalizedString("lb.info"));
  335. typValue = new JLabel();
  336. infoValue = new JLabel();
  337. statusValue = new JLabel();
  338. attributes = new JButton(getLocalizedString("bt.attributes"));
  339. attributes.setEnabled(false);
  340. attributes.setActionCommand("ATTRIBUTES");
  341. attributes.addActionListener(this);
  342. services_cob = new JComboBox(getPrintServices());
  343. services_cob.setActionCommand("SERVICE");
  344. services_cob.addActionListener(this);
  345. fileRedirection_cb = new JCheckBox(getLocalizedString("cb.output"));
  346. fileRedirection_cb.setEnabled(false);
  347. GridBagLayout layout = new GridBagLayout();
  348. GridBagConstraints c = new GridBagConstraints();
  349. setLayout(layout);
  350. setBorder(new TitledBorder(getLocalizedString("title.printservice")));
  351. c.insets = new Insets(5, 5, 5, 5);
  352. c.anchor = GridBagConstraints.LINE_END;
  353. c.gridx = 0;
  354. c.gridy = 0;
  355. add(name, c);
  356. c.gridx = 0;
  357. c.gridy = 1;
  358. add(status, c);
  359. c.gridx = 0;
  360. c.gridy = 2;
  361. add(typ, c);
  362. c.gridx = 0;
  363. c.gridy = 3;
  364. add(info, c);
  365. c.gridx = 2;
  366. c.gridy = 3;
  367. c.weightx = 1;
  368. add(fileRedirection_cb, c);
  369. c.anchor = GridBagConstraints.LINE_START;
  370. c.fill = GridBagConstraints.HORIZONTAL;
  371. c.gridx = 1;
  372. c.gridy = 0;
  373. c.weightx = 1.5;
  374. add(services_cob, c);
  375. c.gridx = 1;
  376. c.gridy = 1;
  377. c.gridwidth = 2;
  378. c.weightx = 1;
  379. add(statusValue, c);
  380. c.gridx = 1;
  381. c.gridy = 2;
  382. c.gridwidth = 2;
  383. c.weightx = 1;
  384. add(typValue, c);
  385. c.gridx = 1;
  386. c.gridy = 3;
  387. c.gridwidth = 2;
  388. c.weightx = 1;
  389. add(infoValue, c);
  390. c.gridx = 2;
  391. c.gridy = 0;
  392. c.weightx = 1.5;
  393. add(attributes, c);
  394. }
  395. public void actionPerformed(ActionEvent e)
  396. {
  397. if (e.getActionCommand().equals("SERVICE"))
  398. {
  399. setSelectedPrintService((PrintService) services_cob.getSelectedItem());
  400. updateAll();
  401. }
  402. else if (e.getActionCommand().equals("ATTRIBUTES"))
  403. {
  404. // TODO LowPriority-Enhancement: As tests have shown this button
  405. // is even gray and not enabled under Windows - Its a good place
  406. // to provide a classpath specific browsing dialog for all
  407. // attributes not in the default printing dialog.
  408. }
  409. }
  410. /**
  411. * Called to update for new selected
  412. * print service. Tests if currently
  413. * selected attributes are supported.
  414. */
  415. void updateForSelectedService()
  416. {
  417. PrinterMakeAndModel att1 =
  418. getSelectedPrintService().getAttribute(PrinterMakeAndModel.class);
  419. typValue.setText(att1 == null ? "" : att1.getValue());
  420. PrinterInfo att2 =
  421. getSelectedPrintService().getAttribute(PrinterInfo.class);
  422. infoValue.setText(att2 == null ? "" : att2.getValue());
  423. PrinterIsAcceptingJobs att3 =
  424. getSelectedPrintService().getAttribute(PrinterIsAcceptingJobs.class);
  425. PrinterState att4 =
  426. getSelectedPrintService().getAttribute(PrinterState.class);
  427. String status = att4.toString();
  428. if (att3 == PrinterIsAcceptingJobs.ACCEPTING_JOBS)
  429. status += " - " + getLocalizedString("lb.acceptingjobs");
  430. else if (att3 == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS)
  431. status += " - " + getLocalizedString("lb.notacceptingjobs");
  432. statusValue.setText(status);
  433. if (categorySupported(Destination.class))
  434. {
  435. fileRedirection_cb.setEnabled(false);
  436. }
  437. }
  438. }
  439. private PrintServices printserv_panel;
  440. private PrintRange printrange_panel;
  441. private CopiesAndSorted copies;
  442. /**
  443. * Constructs the General Panel.
  444. */
  445. public GeneralPanel()
  446. {
  447. setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  448. printserv_panel = new PrintServices();
  449. printrange_panel = new PrintRange();
  450. copies = new CopiesAndSorted();
  451. JPanel layout_panel = new JPanel();
  452. layout_panel.setLayout(new BoxLayout(layout_panel, BoxLayout.LINE_AXIS));
  453. layout_panel.add(printrange_panel);
  454. layout_panel.add(Box.createRigidArea(new Dimension(10, 0)));
  455. layout_panel.add(copies);
  456. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  457. add(printserv_panel);
  458. add(Box.createRigidArea(new Dimension(0, 12)));
  459. add(layout_panel);
  460. }
  461. /**
  462. * Calls update on all internal panels to adjust
  463. * for a new selected print service.
  464. */
  465. void update()
  466. {
  467. printserv_panel.updateForSelectedService();
  468. printrange_panel.updateForSelectedService();
  469. copies.updateForSelectedService();
  470. }
  471. }
  472. /**
  473. * The Page setup Panel.
  474. * @author Wolfgang Baer (WBaer@gmx.de)
  475. */
  476. final class PageSetupPanel extends JPanel
  477. {
  478. /**
  479. * Handles the orientation attribute.
  480. * @author Wolfgang Baer (WBaer@gmx.de)
  481. */
  482. final class Orientation extends JPanel implements ActionListener
  483. {
  484. private JRadioButton portrait, landscape, rev_portrait, rev_landscape;
  485. Orientation()
  486. {
  487. portrait = new JRadioButton(getLocalizedString("rbt.portrait"));
  488. portrait.addActionListener(this);
  489. landscape = new JRadioButton(getLocalizedString("rbt.landscape"));
  490. landscape.addActionListener(this);
  491. rev_portrait = new JRadioButton(getLocalizedString("rbt.revportrait"));
  492. rev_portrait.addActionListener(this);
  493. rev_landscape = new JRadioButton(getLocalizedString("rbt.revlandscape"));
  494. rev_landscape.addActionListener(this);
  495. ButtonGroup group = new ButtonGroup();
  496. group.add(portrait);
  497. group.add(landscape);
  498. group.add(rev_portrait);
  499. group.add(rev_landscape);
  500. GridBagLayout layout = new GridBagLayout();
  501. GridBagConstraints c = new GridBagConstraints();
  502. c.fill = GridBagConstraints.BOTH;
  503. setLayout(layout);
  504. setBorder(new TitledBorder(getLocalizedString("title.orientation")));
  505. c.insets = new Insets(5, 5, 5, 5);
  506. c.gridx = 0;
  507. c.gridy = 0;
  508. add(portrait, c);
  509. c.gridx = 0;
  510. c.gridy = 1;
  511. add(landscape, c);
  512. c.gridx = 0;
  513. c.gridy = 2;
  514. add(rev_portrait, c);
  515. c.gridx = 0;
  516. c.gridy = 3;
  517. add(rev_landscape, c);
  518. }
  519. // event handling orientation
  520. public void actionPerformed(ActionEvent e)
  521. {
  522. if (e.getSource() == portrait)
  523. atts.add(OrientationRequested.PORTRAIT);
  524. else if (e.getSource() == landscape)
  525. atts.add(OrientationRequested.LANDSCAPE);
  526. else if (e.getSource() == rev_portrait)
  527. atts.add(OrientationRequested.REVERSE_PORTRAIT);
  528. else
  529. atts.add(OrientationRequested.REVERSE_LANDSCAPE);
  530. }
  531. /**
  532. * Called to update for new selected
  533. * print service. Tests if currently
  534. * selected attributes are supported.
  535. */
  536. void updateForSelectedService()
  537. {
  538. if (categorySupported(OrientationRequested.class))
  539. {
  540. portrait.setEnabled(true);
  541. landscape.setEnabled(true);
  542. rev_landscape.setEnabled(true);
  543. rev_portrait.setEnabled(true);
  544. Attribute orientation = attribute(OrientationRequested.class);
  545. if (orientation != null)
  546. {
  547. if (orientation.equals(OrientationRequested.LANDSCAPE))
  548. landscape.setSelected(true);
  549. else if (orientation.equals(OrientationRequested.PORTRAIT))
  550. portrait.setSelected(true);
  551. else if (orientation.equals(OrientationRequested.REVERSE_PORTRAIT))
  552. rev_portrait.setSelected(true);
  553. else
  554. rev_landscape.setSelected(true);
  555. }
  556. else
  557. {
  558. Object defaultValue = defaultValue(OrientationRequested.class);
  559. if (defaultValue.equals(OrientationRequested.LANDSCAPE))
  560. landscape.setSelected(true);
  561. else if (defaultValue.equals(OrientationRequested.PORTRAIT))
  562. portrait.setSelected(true);
  563. else if (defaultValue.equals(OrientationRequested.REVERSE_PORTRAIT))
  564. rev_portrait.setSelected(true);
  565. else
  566. rev_landscape.setSelected(true);
  567. }
  568. }
  569. else
  570. {
  571. portrait.setEnabled(false);
  572. landscape.setEnabled(false);
  573. rev_landscape.setEnabled(false);
  574. rev_portrait.setEnabled(false);
  575. }
  576. }
  577. }
  578. /**
  579. * Handles the media attribute.
  580. * @author Wolfgang Baer (WBaer@gmx.de)
  581. */
  582. final class MediaTypes extends JPanel implements ActionListener
  583. {
  584. private JLabel size_lb, source_lb;
  585. private JComboBox size, source;
  586. MediaTypes()
  587. {
  588. size_lb = new JLabel(getLocalizedString("lb.size"));
  589. source_lb = new JLabel(getLocalizedString("lb.source"));
  590. size = new JComboBox();
  591. size.setEditable(false);
  592. size.addActionListener(this);
  593. source = new JComboBox();
  594. source.setEditable(false);
  595. size.addActionListener(this);
  596. GridBagLayout layout = new GridBagLayout();
  597. GridBagConstraints c = new GridBagConstraints();
  598. setLayout(layout);
  599. setBorder(new TitledBorder(getLocalizedString("title.medias")));
  600. c.insets = new Insets(5, 5, 5, 5);
  601. c.anchor = GridBagConstraints.LINE_END;
  602. c.gridx = 0;
  603. c.gridy = 0;
  604. add(size_lb, c);
  605. c.gridx = 0;
  606. c.gridy = 1;
  607. add(source_lb, c);
  608. c.anchor = GridBagConstraints.LINE_START;
  609. c.fill = GridBagConstraints.HORIZONTAL;
  610. c.gridx = 1;
  611. c.gridy = 0;
  612. c.weightx = 1.5;
  613. add(size, c);
  614. c.gridx = 1;
  615. c.gridy = 1;
  616. c.weightx = 1.5;
  617. add(source, c);
  618. }
  619. public void actionPerformed(ActionEvent event)
  620. {
  621. if (event.getSource() == size)
  622. {
  623. Object obj = size.getSelectedItem();
  624. if (obj instanceof Media)
  625. atts.add((Media) obj);
  626. }
  627. // we ignore source events currently
  628. // as only the automatic selection is used.
  629. }
  630. /**
  631. * Called to update for new selected
  632. * print service. Tests if currently
  633. * selected attributes are supported.
  634. */
  635. void updateForSelectedService()
  636. {
  637. if (categorySupported(Media.class))
  638. {
  639. Media[] medias = (Media[]) getSelectedPrintService()
  640. .getSupportedAttributeValues(Media.class, flavor, null);
  641. size.removeAllItems();
  642. if (medias.length == 0)
  643. size.addItem(getLocalizedString("lb.automatically"));
  644. else
  645. for (int i=0; i < medias.length; i++)
  646. size.addItem(medias[i]);
  647. Media media = (Media) attribute(Media.class);
  648. if (media != null)
  649. size.setSelectedItem(media);
  650. // this is currently ignored
  651. source.removeAllItems();
  652. source.addItem(getLocalizedString("lb.automatically"));
  653. }
  654. else
  655. {
  656. size.removeAllItems();
  657. source.removeAllItems();
  658. size.addItem(getLocalizedString("lb.automatically"));
  659. source.addItem(getLocalizedString("lb.automatically"));
  660. }
  661. }
  662. }
  663. /**
  664. * Handles the media printable area attribute.
  665. * @author Wolfgang Baer (WBaer@gmx.de)
  666. */
  667. final class Margins extends JPanel implements FocusListener
  668. {
  669. private JLabel left, right, top, bottom;
  670. private JTextField left_tf, right_tf, top_tf, bottom_tf;
  671. Margins()
  672. {
  673. left = new JLabel(getLocalizedString("lb.left"));
  674. right = new JLabel(getLocalizedString("lb.right"));
  675. top = new JLabel(getLocalizedString("lb.top"));
  676. bottom = new JLabel(getLocalizedString("lb.bottom"));
  677. left_tf = new JTextField(7);
  678. left_tf.addFocusListener(this);
  679. right_tf = new JTextField(7);
  680. right_tf.addFocusListener(this);
  681. top_tf = new JTextField(7);
  682. top_tf.addFocusListener(this);
  683. bottom_tf = new JTextField(7);
  684. bottom_tf.addFocusListener(this);
  685. GridBagLayout layout = new GridBagLayout();
  686. GridBagConstraints c = new GridBagConstraints();
  687. setLayout(layout);
  688. setBorder(new TitledBorder(getLocalizedString("title.margins")));
  689. c.insets = new Insets(5, 5, 5, 5);
  690. c.gridx = 0;
  691. c.gridy = 0;
  692. add(left, c);
  693. c.gridx = 1;
  694. c.gridy = 0;
  695. add(right, c);
  696. c.insets = new Insets(5, 5, 5, 5);
  697. c.gridx = 0;
  698. c.gridy = 1;
  699. add(left_tf, c);
  700. c.gridx = 1;
  701. c.gridy = 1;
  702. add(right_tf, c);
  703. c.insets = new Insets(10, 5, 5, 5);
  704. c.gridx = 0;
  705. c.gridy = 2;
  706. add(top, c);
  707. c.gridx = 1;
  708. c.gridy = 2;
  709. add(bottom, c);
  710. c.insets = new Insets(0, 5, 5, 5);
  711. c.gridx = 0;
  712. c.gridy = 3;
  713. add(top_tf, c);
  714. c.gridx = 1;
  715. c.gridy = 3;
  716. add(bottom_tf, c);
  717. }
  718. public void focusGained(FocusEvent event)
  719. {
  720. updateMargins();
  721. }
  722. public void focusLost(FocusEvent event)
  723. {
  724. updateMargins();
  725. }
  726. // updates the margins after user changed it
  727. private void updateMargins()
  728. {
  729. // We currently do not support this attribute
  730. // as it is not in the IPP spec and therefore not in CUPS
  731. }
  732. /**
  733. * Called to update for new selected
  734. * print service. Tests if currently
  735. * selected attributes are supported.
  736. */
  737. void updateForSelectedService()
  738. {
  739. if (categorySupported(MediaPrintableArea.class))
  740. {
  741. left.setEnabled(true);
  742. right.setEnabled(true);
  743. top.setEnabled(true);
  744. bottom.setEnabled(true);
  745. left_tf.setEnabled(true);
  746. right_tf.setEnabled(true);
  747. top_tf.setEnabled(true);
  748. bottom_tf.setEnabled(true);
  749. }
  750. else
  751. {
  752. left.setEnabled(false);
  753. right.setEnabled(false);
  754. top.setEnabled(false);
  755. bottom.setEnabled(false);
  756. left_tf.setEnabled(false);
  757. right_tf.setEnabled(false);
  758. top_tf.setEnabled(false);
  759. bottom_tf.setEnabled(false);
  760. }
  761. }
  762. }
  763. private MediaTypes media_panel;
  764. private Orientation orientation_panel;
  765. private Margins margins_panel;
  766. /**
  767. * Constructs the page setup user interface.
  768. */
  769. public PageSetupPanel()
  770. {
  771. setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  772. media_panel = new MediaTypes();
  773. orientation_panel = new Orientation();
  774. margins_panel = new Margins();
  775. JPanel layout_panel = new JPanel();
  776. layout_panel.setLayout(new BoxLayout(layout_panel, BoxLayout.LINE_AXIS));
  777. layout_panel.add(orientation_panel);
  778. layout_panel.add(Box.createRigidArea(new Dimension(10, 0)));
  779. layout_panel.add(margins_panel);
  780. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  781. add(media_panel);
  782. add(Box.createRigidArea(new Dimension(0, 12)));
  783. add(layout_panel);
  784. }
  785. /**
  786. * Calls update on all internal panels to adjust
  787. * for a new selected print service.
  788. */
  789. void update()
  790. {
  791. media_panel.updateForSelectedService();
  792. orientation_panel.updateForSelectedService();
  793. margins_panel.updateForSelectedService();
  794. }
  795. }
  796. /**
  797. * The Appearance panel for quality, color etc.
  798. * @author Wolfgang Baer (WBaer@gmx.de)
  799. */
  800. final class AppearancePanel extends JPanel
  801. {
  802. /**
  803. * Handles the print quality attribute.
  804. * @author Wolfgang Baer (WBaer@gmx.de)
  805. */
  806. final class Quality extends JPanel implements ActionListener
  807. {
  808. private JRadioButton low, normal, high;
  809. private ButtonGroup group;
  810. Quality()
  811. {
  812. low = new JRadioButton(getLocalizedString("rbt.low"));
  813. low.addActionListener(this);
  814. normal = new JRadioButton(getLocalizedString("rbt.normal"));
  815. normal.addActionListener(this);
  816. high = new JRadioButton(getLocalizedString("rbt.high"));
  817. high.addActionListener(this);
  818. group = new ButtonGroup();
  819. group.add(low);
  820. group.add(normal);
  821. group.add(high);
  822. GridBagLayout layout = new GridBagLayout();
  823. GridBagConstraints c = new GridBagConstraints();
  824. setLayout(layout);
  825. setBorder(new TitledBorder(getLocalizedString("title.quality")));
  826. c.fill = GridBagConstraints.HORIZONTAL;
  827. c.insets = new Insets(5, 5, 5, 5);
  828. c.gridx = 0;
  829. c.gridy = 0;
  830. add(low, c);
  831. c.gridx = 0;
  832. c.gridy = 1;
  833. add(normal, c);
  834. c.gridx = 0;
  835. c.gridy = 2;
  836. add(high, c);
  837. }
  838. public void actionPerformed(ActionEvent e)
  839. {
  840. if (e.getSource() == low)
  841. atts.add(PrintQuality.DRAFT);
  842. else if (e.getSource() == normal)
  843. atts.add(PrintQuality.NORMAL);
  844. else
  845. atts.add(PrintQuality.HIGH);
  846. }
  847. /**
  848. * Called to update for new selected
  849. * print service. Tests if currently
  850. * selected attributes are supported.
  851. */
  852. void updateForSelectedService()
  853. {
  854. if (categorySupported(PrintQuality.class))
  855. {
  856. low.setEnabled(true);
  857. normal.setEnabled(true);
  858. high.setEnabled(true);
  859. Object defaultValue = defaultValue(PrintQuality.class);
  860. Attribute quality = attribute(PrintQuality.class);
  861. if (quality != null)
  862. {
  863. if (quality.equals(PrintQuality.DRAFT))
  864. low.setSelected(true);
  865. else if (quality.equals(PrintQuality.NORMAL))
  866. normal.setSelected(true);
  867. else
  868. high.setSelected(true);
  869. }
  870. else
  871. {
  872. if (defaultValue.equals(PrintQuality.DRAFT))
  873. low.setSelected(true);
  874. else if (defaultValue.equals(PrintQuality.NORMAL))
  875. normal.setSelected(true);
  876. else
  877. high.setSelected(true);
  878. }
  879. }
  880. else
  881. {
  882. low.setEnabled(false);
  883. normal.setEnabled(false);
  884. high.setEnabled(false);
  885. }
  886. }
  887. }
  888. /**
  889. * Handles the job attributes as requesting username, jobname etc.
  890. * @author Wolfgang Baer (WBaer@gmx.de)
  891. */
  892. final class JobAttributes extends JPanel
  893. implements ActionListener, ChangeListener, FocusListener
  894. {
  895. private JLabel jobname, username, priority_lb;
  896. private JTextField jobname_tf, username_tf;
  897. private JCheckBox cover;
  898. private JSpinner priority;
  899. private SpinnerNumberModel model;
  900. JobAttributes()
  901. {
  902. jobname = new JLabel(getLocalizedString("lb.jobname"));
  903. username = new JLabel(getLocalizedString("lb.username"));
  904. priority_lb = new JLabel(getLocalizedString("lb.priority"));
  905. cover = new JCheckBox(getLocalizedString("cb.cover"));
  906. cover.addActionListener(this);
  907. model = new SpinnerNumberModel(1, 1, 100, 1);
  908. priority = new JSpinner(model);
  909. priority.addChangeListener(this);
  910. jobname_tf = new JTextField();
  911. jobname_tf.addFocusListener(this);
  912. username_tf = new JTextField();
  913. username_tf.addFocusListener(this);
  914. GridBagLayout layout = new GridBagLayout();
  915. GridBagConstraints c = new GridBagConstraints();
  916. setLayout(layout);
  917. setBorder(new TitledBorder(getLocalizedString("title.jobattributes")));
  918. c.insets = new Insets(10, 5, 10, 5);
  919. c.gridx = 0;
  920. c.gridy = 0;
  921. add(cover, c);
  922. c.anchor = GridBagConstraints.LINE_END;
  923. c.gridx = 1;
  924. c.gridy = 0;
  925. c.weightx = 2;
  926. add(priority_lb, c);
  927. c.gridx = 2;
  928. c.gridy = 0;
  929. c.weightx = 0.5;
  930. add(priority, c);
  931. c.anchor = GridBagConstraints.LINE_END;
  932. c.gridx = 0;
  933. c.gridy = 1;
  934. add(jobname, c);
  935. c.gridx = 0;
  936. c.gridy = 2;
  937. add(username, c);
  938. c.anchor = GridBagConstraints.CENTER;
  939. c.fill = GridBagConstraints.HORIZONTAL;
  940. c.gridx = 1;
  941. c.gridy = 1;
  942. c.gridwidth = 2;
  943. c.weightx = 1.5;
  944. add(jobname_tf, c);
  945. c.insets = new Insets(10, 5, 15, 5);
  946. c.gridx = 1;
  947. c.gridy = 2;
  948. add(username_tf, c);
  949. }
  950. public void actionPerformed(ActionEvent event)
  951. {
  952. if (cover.isSelected())
  953. atts.add(JobSheets.STANDARD);
  954. else
  955. atts.add(JobSheets.NONE);
  956. }
  957. public void stateChanged(ChangeEvent event)
  958. {
  959. int value = ((Integer) priority.getValue()).intValue();
  960. atts.add(new JobPriority(value));
  961. }
  962. public void focusGained(FocusEvent event)
  963. {
  964. updateTextfields(event);
  965. }
  966. public void focusLost(FocusEvent event)
  967. {
  968. updateTextfields(event);
  969. }
  970. private void updateTextfields(FocusEvent event)
  971. {
  972. if (event.getSource() == jobname_tf)
  973. atts.add(new JobName(jobname_tf.getText(), null));
  974. else
  975. atts.add(new RequestingUserName(username_tf.getText(), null));
  976. }
  977. /**
  978. * Called to update for new selected
  979. * print service. Tests if currently
  980. * selected attributes are supported.
  981. */
  982. void updateForSelectedService()
  983. {
  984. // JobPriority
  985. if (categorySupported(JobPriority.class))
  986. {
  987. JobPriority prio = (JobPriority) attribute(JobPriority.class);
  988. JobPriority value = (JobPriority) defaultValue(JobPriority.class);
  989. priority.setEnabled(true);
  990. if (prio != null)
  991. model.setValue(new Integer(prio.getValue()));
  992. else
  993. model.setValue(new Integer(value.getValue()));
  994. }
  995. else
  996. priority.setEnabled(false);
  997. // Requesting username
  998. if (categorySupported(RequestingUserName.class))
  999. {
  1000. Attribute user = attribute(RequestingUserName.class);
  1001. Object value = defaultValue(RequestingUserName.class);
  1002. username.setEnabled(true);
  1003. if (user != null)
  1004. username_tf.setText(user.toString());
  1005. else
  1006. username_tf.setText(value.toString());
  1007. }
  1008. else
  1009. username.setEnabled(false);
  1010. // Job Name
  1011. if (categorySupported(JobName.class))
  1012. {
  1013. Attribute job = attribute(JobName.class);
  1014. Object value = defaultValue(JobName.class);
  1015. jobname.setEnabled(true);
  1016. if (job != null)
  1017. jobname_tf.setText(job.toString());
  1018. else
  1019. jobname_tf.setText(value.toString());
  1020. }
  1021. else
  1022. jobname.setEnabled(false);
  1023. // Job sheets
  1024. if (categorySupported(JobSheets.class))
  1025. {
  1026. Attribute sheet = attribute(JobSheets.class);
  1027. Object value = defaultValue(JobSheets.class);
  1028. cover.setEnabled(true);
  1029. if (sheet != null)
  1030. {
  1031. if (sheet.equals(JobSheets.NONE))
  1032. cover.setSelected(false);
  1033. else
  1034. cover.setSelected(true);
  1035. }
  1036. else
  1037. {
  1038. if (value.equals(JobSheets.NONE))
  1039. cover.setSelected(false);
  1040. else
  1041. cover.setSelected(true);
  1042. }
  1043. }
  1044. else
  1045. cover.setEnabled(false);
  1046. }
  1047. }
  1048. /**
  1049. * Handles the sides attributes.
  1050. * @author Wolfgang Baer (WBaer@gmx.de)
  1051. */
  1052. final class SidesPanel extends JPanel implements ActionListener
  1053. {
  1054. private JRadioButton oneside, calendar, duplex;
  1055. SidesPanel()
  1056. {
  1057. oneside = new JRadioButton(getLocalizedString("rbt.onesided"));
  1058. oneside.addActionListener(this);
  1059. calendar = new JRadioButton(getLocalizedString("rbt.calendar"));
  1060. calendar.addActionListener(this);
  1061. duplex = new JRadioButton(getLocalizedString("rbt.duplex"));
  1062. duplex.addActionListener(this);
  1063. ButtonGroup group = new ButtonGroup();
  1064. group.add(oneside);
  1065. group.add(calendar);
  1066. group.add(duplex);
  1067. GridBagLayout layout = new GridBagLayout();
  1068. GridBagConstraints c = new GridBagConstraints();
  1069. c.fill = GridBagConstraints.BOTH;
  1070. setLayout(layout);
  1071. setBorder(new TitledBorder(getLocalizedString("title.sides")));
  1072. c.insets = new Insets(5, 5, 5, 5);
  1073. c.gridx = 0;
  1074. c.gridy = 0;
  1075. add(oneside, c);
  1076. c.gridx = 0;
  1077. c.gridy = 1;
  1078. add(calendar, c);
  1079. c.gridx = 0;
  1080. c.gridy = 2;
  1081. add(duplex, c);
  1082. }
  1083. public void actionPerformed(ActionEvent e)
  1084. {
  1085. if (e.getSource() == calendar)
  1086. atts.add(Sides.TWO_SIDED_SHORT_EDGE);
  1087. else if (e.getSource() == oneside)
  1088. atts.add(Sides.ONE_SIDED);
  1089. else
  1090. atts.add(Sides.TWO_SIDED_LONG_EDGE);
  1091. }
  1092. /**
  1093. * Called to update for new selected
  1094. * print service. Tests if currently
  1095. * selected attributes are supported.
  1096. */
  1097. void updateForSelectedService()
  1098. {
  1099. if (categorySupported(Sides.class))
  1100. {
  1101. oneside.setEnabled(true);
  1102. calendar.setEnabled(true);
  1103. duplex.setEnabled(true);
  1104. Object defaultValue = defaultValue(Sides.class);
  1105. Attribute sides = attribute(Sides.class);
  1106. if (sides != null)
  1107. {
  1108. if (sides.equals(Sides.TWO_SIDED_SHORT_EDGE))
  1109. calendar.setSelected(true);
  1110. else if (sides.equals(Sides.ONE_SIDED))
  1111. oneside.setSelected(true);
  1112. else
  1113. duplex.setSelected(true);
  1114. }
  1115. else
  1116. {
  1117. if (defaultValue.equals(Sides.TWO_SIDED_SHORT_EDGE))
  1118. calendar.setSelected(true);
  1119. else if (defaultValue.equals(Sides.ONE_SIDED))
  1120. oneside.setSelected(true);
  1121. else
  1122. duplex.setSelected(true);
  1123. }
  1124. }
  1125. else
  1126. {
  1127. oneside.setEnabled(false);
  1128. calendar.setEnabled(false);
  1129. duplex.setEnabled(false);
  1130. }
  1131. }
  1132. }
  1133. /**
  1134. * Handles the chromaticity attributes.
  1135. * @author Wolfgang Baer (WBaer@gmx.de)
  1136. */
  1137. final class Color extends JPanel implements ActionListener
  1138. {
  1139. private JRadioButton bw, color;
  1140. Color()
  1141. {
  1142. bw = new JRadioButton(getLocalizedString("rbt.blackwhite"));
  1143. bw.addActionListener(this);
  1144. color = new JRadioButton(getLocalizedString("rbt.color"));
  1145. color.addActionListener(this);
  1146. ButtonGroup group = new ButtonGroup();
  1147. group.add(bw);
  1148. group.add(color);
  1149. GridBagLayout layout = new GridBagLayout();
  1150. GridBagConstraints c = new GridBagConstraints();
  1151. setLayout(layout);
  1152. setBorder(new TitledBorder(getLocalizedString("title.color")));
  1153. c.fill = GridBagConstraints.HORIZONTAL;
  1154. c.insets = new Insets(5, 5, 5, 5);
  1155. c.gridx = 0;
  1156. c.gridy = 0;
  1157. add(bw, c);
  1158. c.gridx = 0;
  1159. c.gridy = 1;
  1160. add(color, c);
  1161. }
  1162. public void actionPerformed(ActionEvent e)
  1163. {
  1164. if (e.getSource() == bw)
  1165. atts.add(Chromaticity.MONOCHROME);
  1166. else
  1167. atts.add(Chromaticity.COLOR);
  1168. }
  1169. /**
  1170. * Called to update for new selected
  1171. * print service. Tests if currently
  1172. * selected attributes are supported.
  1173. */
  1174. void updateForSelectedService()
  1175. {
  1176. if (categorySupported(Chromaticity.class))
  1177. {
  1178. bw.setEnabled(true);
  1179. color.setEnabled(true);
  1180. Object defaultValue = defaultValue(Chromaticity.class);
  1181. Attribute chromaticity = attribute(Chromaticity.class);
  1182. if (chromaticity != null)
  1183. {
  1184. if (chromaticity.equals(Chromaticity.MONOCHROME))
  1185. bw.setSelected(true);
  1186. else
  1187. color.setSelected(true);
  1188. }
  1189. else
  1190. {
  1191. if (defaultValue.equals(Chromaticity.MONOCHROME))
  1192. bw.setSelected(true);
  1193. else
  1194. color.setSelected(true);
  1195. }
  1196. }
  1197. else
  1198. {
  1199. bw.setEnabled(false);
  1200. color.setEnabled(false);
  1201. }
  1202. }
  1203. }
  1204. private Quality quality_panel;
  1205. private JobAttributes jobAttr_panel;
  1206. private SidesPanel sides_panel;
  1207. private Color chromaticy_panel;
  1208. /**
  1209. * Creates the panel for appearance attributes.
  1210. */
  1211. public AppearancePanel()
  1212. {
  1213. setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  1214. quality_panel = new Quality();
  1215. jobAttr_panel = new JobAttributes();
  1216. sides_panel = new SidesPanel();
  1217. chromaticy_panel = new Color();
  1218. JPanel layout_panel = new JPanel();
  1219. layout_panel.setLayout(new BoxLayout(layout_panel, BoxLayout.LINE_AXIS));
  1220. layout_panel.add(chromaticy_panel);
  1221. layout_panel.add(Box.createRigidArea(new Dimension(10, 0)));
  1222. layout_panel.add(quality_panel);
  1223. JPanel layout2_panel = new JPanel();
  1224. layout2_panel.setLayout(new BoxLayout(layout2_panel, BoxLayout.LINE_AXIS));
  1225. layout2_panel.add(sides_panel);
  1226. layout2_panel.add(Box.createRigidArea(new Dimension(10, 0)));
  1227. layout2_panel.add(jobAttr_panel);
  1228. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  1229. add(layout_panel);
  1230. add(Box.createRigidArea(new Dimension(0, 12)));
  1231. add(layout2_panel);
  1232. }
  1233. /**
  1234. * Calls update on all internal panels to adjust
  1235. * for a new selected print service.
  1236. */
  1237. void update()
  1238. {
  1239. quality_panel.updateForSelectedService();
  1240. jobAttr_panel.updateForSelectedService();
  1241. sides_panel.updateForSelectedService();
  1242. chromaticy_panel.updateForSelectedService();
  1243. }
  1244. }
  1245. // on main contentpane
  1246. private JButton ok_bt;
  1247. private JButton cancel_bt;
  1248. // the tabs
  1249. private GeneralPanel general_panel;
  1250. private PageSetupPanel pagesetup_panel;
  1251. private AppearancePanel appearance_panel;
  1252. private PrintService[] services;
  1253. private PrintService defaultService;
  1254. private PrintService selectedService;
  1255. private DocFlavor flavor;
  1256. private PrintRequestAttributeSet attributes;
  1257. private boolean onlyPageDialog;
  1258. private PrintRequestAttributeSet atts;
  1259. private final static ResourceBundle messages
  1260. = ResourceBundle.getBundle("gnu.javax.print.MessagesBundle");
  1261. // TODO LowPriority: Include checks so that if a specific value formerly
  1262. // selected is no more supported by the new service changes to the default.
  1263. /**
  1264. * Class private constructs a printer dialog.
  1265. *
  1266. * @param gc the screen to use. <code>null</code> is default screen.
  1267. * @param services the print services to browse (not null).
  1268. * @param defaultService the default service. If <code>null</code>
  1269. * the first of the print services in the services array will be used.
  1270. * @param flavor the flavours to be printed.
  1271. * @param attributes the attributes requested. Will be updated
  1272. * by selections done by the user in the dialog.
  1273. * @param onlyPageDialog if true a page settings only dialog is constructed.
  1274. *
  1275. * @throws HeadlessException if GraphicsEnvironment is headless
  1276. */
  1277. private PrinterDialog(GraphicsConfiguration gc, PrintService[] services,
  1278. PrintService defaultService, DocFlavor flavor,
  1279. PrintRequestAttributeSet attributes, boolean onlyPageDialog, String title)
  1280. throws HeadlessException
  1281. {
  1282. super((Frame)null, title, true, gc);
  1283. setResizable(false);
  1284. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  1285. // check and remove service not supporting the flavor
  1286. if (flavor != null)
  1287. {
  1288. ArrayList list = new ArrayList(services.length);
  1289. for(int i=0; i < services.length; i++)
  1290. if (services[i].isDocFlavorSupported(flavor))
  1291. list.add(services[i]);
  1292. if (defaultService != null
  1293. && (! list.contains(defaultService)))
  1294. defaultService = (PrintService) list.get(0);
  1295. PrintService[] newServices = new PrintService[list.size()];
  1296. this.services = (PrintService[]) list.toArray(newServices);
  1297. }
  1298. else
  1299. this.services = services;
  1300. if (defaultService == null)
  1301. this.defaultService = services[0];
  1302. else
  1303. this.defaultService = defaultService;
  1304. this.selectedService = this.defaultService;
  1305. this.flavor = flavor;
  1306. // the attributes given by the user
  1307. this.attributes = attributes;
  1308. // the one to work with during browsing
  1309. this.atts = new HashPrintRequestAttributeSet(attributes);
  1310. this.onlyPageDialog = onlyPageDialog;
  1311. initUI(onlyPageDialog);
  1312. pack();
  1313. updateAll();
  1314. }
  1315. /**
  1316. * Constructs a page settings only dialog.
  1317. *
  1318. * @param gc the screen to use. <code>null</code> is default screen.
  1319. * @param service the print service for the page dialog.
  1320. * the first of the print services in the services array will be used.
  1321. * @param flavor the flavours to be printed.
  1322. * @param attributes the attributes requested. Will be updated
  1323. * by selections done by the user in the dialog.
  1324. *
  1325. * @throws HeadlessException if GraphicsEnvironment is headless
  1326. */
  1327. public PrinterDialog(GraphicsConfiguration gc, PrintService service,
  1328. DocFlavor flavor, PrintRequestAttributeSet attributes)
  1329. throws HeadlessException
  1330. {
  1331. this(gc, new PrintService[] {service}, service, flavor, attributes,
  1332. true, getLocalizedString("title.pagedialog"));
  1333. }
  1334. /**
  1335. * Constructs a printer dialog.
  1336. *
  1337. * @param gc the screen to use. <code>null</code> is default screen.
  1338. * @param services the print services to browse (not null).
  1339. * @param defaultService the default service. If <code>null</code>
  1340. * the first of the print services in the services array will be used.
  1341. * @param flavor the flavours to be printed.
  1342. * @param attributes the attributes requested. Will be updated
  1343. * by selections done by the user in the dialog.
  1344. *
  1345. * @throws HeadlessException if GraphicsEnvironment is headless
  1346. */
  1347. public PrinterDialog(GraphicsConfiguration gc, PrintService[] services,
  1348. PrintService defaultService, DocFlavor flavor,
  1349. PrintRequestAttributeSet attributes)
  1350. throws HeadlessException
  1351. {
  1352. this(gc, services, defaultService, flavor, attributes,
  1353. false, getLocalizedString("title.printdialog"));
  1354. }
  1355. // initializes the gui parts
  1356. private void initUI(boolean onlyPageDialog)
  1357. {
  1358. JPanel buttonPane = new JPanel();
  1359. if (onlyPageDialog)
  1360. {
  1361. JPanel pane = new JPanel();
  1362. pane.setLayout(new BorderLayout());
  1363. pagesetup_panel = new PageSetupPanel();
  1364. pane.add(pagesetup_panel, BorderLayout.CENTER);
  1365. ok_bt = new JButton(getLocalizedString("bt.OK"));
  1366. ok_bt.addActionListener(this);
  1367. cancel_bt = new JButton(getLocalizedString("bt.cancel"));
  1368. cancel_bt.addActionListener(this);
  1369. getContentPane().add(pane, BorderLayout.CENTER);
  1370. }
  1371. else
  1372. {
  1373. general_panel = new GeneralPanel();
  1374. pagesetup_panel = new PageSetupPanel();
  1375. appearance_panel = new AppearancePanel();
  1376. JTabbedPane pane = new JTabbedPane();
  1377. pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  1378. ok_bt = new JButton(getLocalizedString("bt.print"));
  1379. ok_bt.addActionListener(this);
  1380. cancel_bt = new JButton(getLocalizedString("bt.cancel"));
  1381. cancel_bt.addActionListener(this);
  1382. // populate jtabbedpane
  1383. pane.addTab(getLocalizedString("tab.general"), general_panel);
  1384. pane.addTab(getLocalizedString("tab.pagesetup"), pagesetup_panel);
  1385. pane.addTab(getLocalizedString("tab.appearance"), appearance_panel);
  1386. // Put everything together
  1387. getContentPane().add(pane, BorderLayout.CENTER);
  1388. }
  1389. buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
  1390. buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  1391. buttonPane.add(Box.createHorizontalGlue());
  1392. buttonPane.add(ok_bt);
  1393. buttonPane.add(Box.createRigidArea(new Dimension(5, 0)));
  1394. buttonPane.add(cancel_bt);
  1395. getContentPane().add(buttonPane, BorderLayout.PAGE_END);
  1396. }
  1397. /**
  1398. * Returns the modified attributes set.
  1399. * @return The attributes.
  1400. */
  1401. public PrintRequestAttributeSet getAttributes()
  1402. {
  1403. return attributes;
  1404. }
  1405. /**
  1406. * Returns the print service selected by the user.
  1407. * @return The selected print service.
  1408. */
  1409. public PrintService getSelectedPrintService()
  1410. {
  1411. return selectedService;
  1412. }
  1413. /**
  1414. * Sets the currently selected print service.
  1415. *
  1416. * @param service the service selected.
  1417. */
  1418. protected void setSelectedPrintService(PrintService service)
  1419. {
  1420. selectedService = service;
  1421. }
  1422. /**
  1423. * Returns the print service array.
  1424. * @return The print services.
  1425. */
  1426. protected PrintService[] getPrintServices()
  1427. {
  1428. return services;
  1429. }
  1430. /**
  1431. * Calls update on all panels to adjust
  1432. * for a new selected print service.
  1433. */
  1434. void updateAll()
  1435. {
  1436. pagesetup_panel.update();
  1437. if (! onlyPageDialog)
  1438. {
  1439. general_panel.update();
  1440. appearance_panel.update();
  1441. }
  1442. }
  1443. boolean categorySupported(Class category)
  1444. {
  1445. return getSelectedPrintService().
  1446. isAttributeCategorySupported(category);
  1447. }
  1448. Object defaultValue(Class category)
  1449. {
  1450. return getSelectedPrintService().
  1451. getDefaultAttributeValue(category);
  1452. }
  1453. Attribute attribute(Class category)
  1454. {
  1455. return atts.get(category);
  1456. }
  1457. /**
  1458. * Action handler for Print/Cancel buttons.
  1459. * If cancel is pressed we reset the attributes
  1460. * and the selected service.
  1461. *
  1462. * @param e the ActionEvent
  1463. */
  1464. public void actionPerformed(ActionEvent e)
  1465. {
  1466. if (e.getSource() == ok_bt)
  1467. {
  1468. setVisible(false);
  1469. attributes.addAll(atts);
  1470. dispose();
  1471. }
  1472. else
  1473. {
  1474. setVisible(false);
  1475. selectedService = null;
  1476. dispose();
  1477. }
  1478. }
  1479. /**
  1480. * Retrieves localized messages from the resource bundle.
  1481. *
  1482. * @param key the key
  1483. * @return The localized value for the key.
  1484. */
  1485. static final String getLocalizedString(String key) {
  1486. return messages.getString(key);
  1487. }
  1488. }