File: GLPoptions.java

package info (click to toggle)
cupsys 1.2.7-4%2Betch9
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,436 kB
  • ctags: 10,404
  • sloc: ansic: 97,130; cpp: 49,167; java: 6,199; sh: 4,723; makefile: 1,918; lisp: 232; perl: 145; python: 119; php: 28
file content (662 lines) | stat: -rw-r--r-- 20,130 bytes parent folder | download | duplicates (3)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.net.URL;
import java.net.*;
import java.io.*;
import com.easysw.cups.*;

public class GLPoptions implements ActionListener 
{
    Cups        cups    = null;
    CupsJob     job     = null;
    CupsPrinter printer = null;

    String fileName    = "";

    JPanel             mainPanel;
    JTextField         fileTextField;
    JButton            printButton;
    GridBagLayout      mainLayout;
    GridBagConstraints mainConst;

    //
    //  Print options;
    //
    String[]            jobSheetsNames;
    String[]		orientationNames;
    int[]		orientationValues;
    String[]		qualityNames;
    int[]		qualityValues;

    String              jobSheetsOption   = "";
    int                 orientationOption = -1;
    int                 qualityOption     = -1;

    int			pageLowerOption   = 0;
    int			pageUpperOption   = 0;
    boolean             pagePrintAll      = true;

    int			numCopiesOption   = 1;
    int			numLowerCopiesOption   = -1;
    int			numUpperCopiesOption   = -1;

    int                 selectedJobSheets   = 0;
    int                 selectedOrientation = 0;
    int                 selectedQuality     = 0;

    JComboBox           orientationBox;
    JComboBox           jobSheetsBox;
    JTextField          numCopiesField;
    JCheckBox           printAllCheckBox;
    JTextField          pageLowerField;
    JTextField          pageUpperField;
    MyTextListener      textListener = new MyTextListener();
 

    // Constructor
    public GLPoptions() 
    {
	mainPanel   = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(GLPcolors.backgroundColor);
        JLabel label = new JLabel("No printer selected");
        label.setForeground(GLPcolors.foregroundColor);
        mainPanel.add(label,BorderLayout.CENTER);
    }

    // Constructor
    public GLPoptions(CupsPrinter cp) 
    {
      printer = cp;
      if (printer != null)
      {
        load(printer);
      }
      else
      {
	mainPanel   = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(GLPcolors.backgroundColor);
        JLabel label = new JLabel("No printer selected");
        label.setForeground(GLPcolors.foregroundColor);
        mainPanel.add(label,BorderLayout.CENTER);
      }
    }


    private void load( CupsPrinter cp )
    {

        fillOptionValues();

	// Create the main panel to contain the two sub panels.
	mainPanel   = new JPanel();
        mainLayout  = new GridBagLayout();
        mainConst   = new GridBagConstraints();

	mainPanel.setLayout(mainLayout);
	mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        mainPanel.setBackground(GLPcolors.backgroundColor);

        String tmp_s = "Printing to " + printer.getPrinterName() + 
                       " on " + GLPvars.cupsServerName;
        JLabel printerNameText = new JLabel(tmp_s);
        printerNameText.setForeground(GLPcolors.foregroundColor);
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 0;
        mainConst.fill      = GridBagConstraints.NONE;
        mainConst.weightx   = 0.0;
        mainConst.weighty   = 0.0;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( printerNameText, mainConst );
        mainPanel.add(printerNameText);

        JPanel filePanel = buildFilePanel();
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 1;
        mainConst.fill      = GridBagConstraints.HORIZONTAL;
        mainConst.weightx   = 1.0;
        mainConst.weighty   = 0.1;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( filePanel, mainConst );
        mainPanel.add(filePanel);

        JPanel orientationPanel = buildOrientationComboBox();
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 2;
        mainConst.fill      = GridBagConstraints.NONE;
        mainConst.weightx   = 0.8;
        mainConst.weighty   = 0.1;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( orientationPanel, mainConst );
        mainPanel.add(orientationPanel);

        JPanel jobSheetsPanel = buildJobSheetsComboBox();
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 3;
        mainConst.fill      = GridBagConstraints.NONE;
        mainConst.weightx   = 0.8;
        mainConst.weighty   = 0.1;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( jobSheetsPanel, mainConst );
        mainPanel.add(jobSheetsPanel);

        JPanel numCopiesPanel = buildNumCopiesPanel();
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 4;
        mainConst.fill      = GridBagConstraints.HORIZONTAL;
        mainConst.weightx   = 1.0;
        mainConst.weighty   = 0.1;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( numCopiesPanel, mainConst );
        mainPanel.add(numCopiesPanel);

        JPanel pageRangePanel = buildPageRangePanel();
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 5;
        mainConst.fill      = GridBagConstraints.HORIZONTAL;
        mainConst.weightx   = 1.0;
        mainConst.weighty   = 0.1;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( pageRangePanel, mainConst );
        mainPanel.add(pageRangePanel);

        JPanel buttonPanel = buildButtonPanel();
        mainConst.gridwidth = GridBagConstraints.RELATIVE;
        mainConst.gridx     = 0;
        mainConst.gridy     = 6;
        mainConst.fill      = GridBagConstraints.NONE;
        mainConst.weightx   = 1.0;
        mainConst.weighty   = 0.1;
        mainConst.ipady     = 4;
        mainLayout.setConstraints( buttonPanel, mainConst );
        mainPanel.add(buttonPanel);

    }


    //  --------------------------------------------------------------
    //
    //  Filename / Browse panel
    //
    public JPanel buildFilePanel()
    {
      JPanel             localPanel    = new JPanel();
      final JFileChooser fc            = new JFileChooser();

      localPanel.setBackground(GLPcolors.backgroundColor);
      localPanel.setLayout(new BorderLayout());

      //Create a regular text field.
      fileTextField = new JTextField(50);
      fileTextField.addActionListener(this);

      //Create some labels for the fields.
      JLabel fileFieldLabel = new JLabel(" File to print: ");
      fileFieldLabel.setForeground(GLPcolors.foregroundColor);
      // fileFieldLabel.setLabelFor(fileTextField);

      localPanel.add( fileFieldLabel, BorderLayout.WEST );
      localPanel.add( fileTextField, BorderLayout.CENTER );

      //Create the open button
      JButton openButton = new JButton("Browse ..." );
      openButton.addActionListener(new ActionListener() 
      {
         public void actionPerformed(ActionEvent e) 
         {
           int returnVal = fc.showOpenDialog(mainPanel);
           if (returnVal == JFileChooser.APPROVE_OPTION) 
           {
             File file = fc.getSelectedFile();
             // fileTextField.setText(file.getPath() + file.getName());
             fileTextField.setText(file.getPath());
             fileName = file.getPath();
           } 
         }
      });
      openButton.setBackground(GLPcolors.buttonBackgroundColor);
      openButton.setForeground(GLPcolors.buttonForegroundColor);
      localPanel.add(openButton, BorderLayout.EAST );
      return(localPanel);
    }



    public JPanel buildOrientationComboBox()
    {
      JPanel localPanel = new JPanel();
      localPanel.setLayout(new BorderLayout());
      localPanel.setBackground(GLPcolors.backgroundColor);

      JLabel localLabel = new JLabel("Page Orientation:  ");
      localLabel.setBackground(GLPcolors.backgroundColor);
      localLabel.setForeground(GLPcolors.foregroundColor);

      orientationBox = new JComboBox(orientationNames);
      if (selectedOrientation > 0)
        orientationBox.setSelectedIndex(selectedOrientation);
      orientationBox.addActionListener(this);
      orientationBox.setBackground(GLPcolors.backgroundColor);

      localPanel.add(localLabel,BorderLayout.WEST);
      localPanel.add(orientationBox,BorderLayout.CENTER);

      return(localPanel);
    }


    public JPanel buildJobSheetsComboBox()
    {
      JPanel localPanel = new JPanel();
      localPanel.setLayout(new BorderLayout());
      localPanel.setBackground(GLPcolors.backgroundColor);

      JLabel localLabel = new JLabel("Job Sheets:  ");
      localLabel.setBackground(GLPcolors.backgroundColor);
      localLabel.setForeground(GLPcolors.foregroundColor);

      jobSheetsBox = new JComboBox(jobSheetsNames);
      if (selectedJobSheets > 0)
        jobSheetsBox.setSelectedIndex(selectedJobSheets);
      jobSheetsBox.addActionListener(this);
      jobSheetsBox.setBackground(GLPcolors.backgroundColor);

      localPanel.add(localLabel,BorderLayout.WEST);
      localPanel.add(jobSheetsBox,BorderLayout.CENTER);

      return(localPanel);
    }

    public JPanel buildNumCopiesPanel()
    {
      JPanel localPanel = new JPanel();
      localPanel.setLayout(new FlowLayout());
      localPanel.setBackground(GLPcolors.backgroundColor);

      JLabel localLabel = new JLabel("Number of copies:  ");
      localLabel.setBackground(GLPcolors.backgroundColor);
      localLabel.setForeground(GLPcolors.foregroundColor);

      numCopiesField = new JTextField(3);
      if (numCopiesOption > 0)
        numCopiesField.setText(new Integer(numCopiesOption).toString());
      numCopiesField.addActionListener(this);
      numCopiesField.addFocusListener(textListener);
      numCopiesField.setBackground(GLPcolors.backgroundColor);

      localPanel.add(localLabel);
      localPanel.add(numCopiesField);

      return(localPanel);
    }


    public JPanel buildPageRangePanel()
    {
      JPanel localPanel = new JPanel();
      localPanel.setLayout(new FlowLayout());
      localPanel.setBackground(GLPcolors.backgroundColor);

      printAllCheckBox = new JCheckBox("Print all", pagePrintAll );
      printAllCheckBox.setBackground(GLPcolors.backgroundColor);
      printAllCheckBox.setForeground(GLPcolors.foregroundColor);
      printAllCheckBox.addActionListener(this);

      JLabel localLabel = new JLabel("-or- pages:  ");
      localLabel.setBackground(GLPcolors.backgroundColor);
      localLabel.setForeground(GLPcolors.foregroundColor);

      JLabel localLabel2 = new JLabel(" to ");
      localLabel2.setBackground(GLPcolors.backgroundColor);
      localLabel2.setForeground(GLPcolors.foregroundColor);

      pageLowerField = new JTextField(4);
      pageUpperField = new JTextField(4);

      pageLowerField.addActionListener(this);
      pageUpperField.addActionListener(this);
      pageLowerField.addFocusListener(textListener);
      pageUpperField.addFocusListener(textListener);

      pageLowerField.setBackground(GLPcolors.backgroundColor);
      pageUpperField.setBackground(GLPcolors.backgroundColor);
      pageLowerField.setEnabled(false);
      pageUpperField.setEnabled(false);

      localPanel.add(printAllCheckBox);
      localPanel.add(localLabel);
      localPanel.add(pageLowerField);
      localPanel.add(localLabel2);
      localPanel.add(pageUpperField);

      return(localPanel);
    }




    public JPanel buildTextPanel()
    {
      JPanel localPanel = new JPanel();
      return(localPanel);
    }

    public JPanel buildButtonPanel()
    {
      JPanel localPanel = new JPanel();
      localPanel.setLayout(new BorderLayout());
      printButton = new JButton(" Print ");
      printButton.setBackground(GLPcolors.buttonBackgroundColor);
      printButton.setForeground(GLPcolors.buttonForegroundColor);
      printButton.addActionListener( this );
      localPanel.add(printButton, BorderLayout.WEST ); 
      return(localPanel);
    }



    public void updateOptions(CupsPrinter cp)
    {
      printer = cp;
      if (printer != null)
      {
        load(printer);
      }
      else
      {
	mainPanel   = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(GLPcolors.backgroundColor);
        JLabel label = new JLabel("No printer selected");
        label.setForeground(GLPcolors.foregroundColor);
        mainPanel.add(label,BorderLayout.CENTER);
      }
    }

    public JPanel getPanel()
    {
      return(mainPanel);
    }




    public CupsJob printFile( String filename )
    {
      Cups    cups;
      CupsJob job;
      URL     u;
      IPPAttribute attrs[];
      
      attrs = buildPrintAttributes();

      // for (int i=0; i < attrs.length; i++)
      //   attrs[i].dump_values();

      try
      {
        u = new URL("http://" + GLPvars.getServerName() + 
                    ":631/printers/" + printer.getPrinterName() );
        cups = new Cups(u);
        cups.setUser(GLPvars.cupsUser);
        cups.setPasswd(GLPvars.cupsPasswd);

        job = cups.cupsPrintFile(filename,attrs);
        return(job);
      }
      catch (IOException e)
      {
        return(null);
      }
    }




    private void fillOptionValues()
    {
      IPPAttribute  a;
      int           i, n;

      //
      //  Job sheets ....
      //
      jobSheetsNames = printer.getJobSheetsSupported();
      if (printer.getJobSheetsDefault() != "none")
      {
        for (i=0; i < jobSheetsNames.length; i++)
          if (jobSheetsNames[i] == printer.getJobSheetsDefault())
            selectedJobSheets = i;
      }

      //
      //  Orientation ....
      //
      orientationNames  = new String[printer.getOrientationSupported().length];
      orientationValues = printer.getOrientationSupported();
      for (i=0; i < printer.getOrientationSupported().length; i++)
      {
        if (orientationValues[i] == printer.getOrientationDefault())
          selectedOrientation = i;
        switch( orientationValues[i] )
        {
          case IPPDefs.PORTRAIT: 
                       orientationNames[i] = "Portrait";
                       break;
          case IPPDefs.LANDSCAPE: 
                       orientationNames[i] = "Landscape";
                       break;
          case IPPDefs.REVERSE_LANDSCAPE: 
                       orientationNames[i] = "Reverse Landscape";
                       break;
          case IPPDefs.REVERSE_PORTRAIT: 
                       orientationNames[i] = "Reverse Portrait";
                       break;
        }
      }

      if (printer.getLowerCopiesSupported() == 
          printer.getUpperCopiesSupported())
      {
        numCopiesOption = printer.getCopiesDefault();        
      }
      else
      {
        numCopiesOption      = printer.getLowerCopiesSupported();
        numLowerCopiesOption = printer.getLowerCopiesSupported();
        numUpperCopiesOption = printer.getUpperCopiesSupported();
      }
    }


    private IPPAttribute[] buildPrintAttributes()
    {
      IPPAttribute      a;
      IPPAttribute[]    attrs;
      int               num_attrs = 0;

      if (orientationOption >= 0)
        num_attrs++;
      if (jobSheetsOption.length() > 0)
        num_attrs++;
      if (numCopiesOption > 1)
        num_attrs++;
      if ((pageLowerOption > 0) && (pageUpperOption > 0) && (!pagePrintAll))
        num_attrs++;

      if (num_attrs > 0)
        attrs = new IPPAttribute[num_attrs];
      else
        return(null);

      int i = 0;
      if (jobSheetsOption.length() > 0)
      {
        attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
                                     IPPDefs.TAG_NAME,
                                     "job-sheets" );
        attrs[i].addString( "", jobSheetsOption );
        i++;
      }
      if (orientationOption >= IPPDefs.PORTRAIT)
      {
        attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
                                     IPPDefs.TAG_ENUM,
                                     "orientation-requested" );
        attrs[i].addEnum( orientationOption );
        i++;
      }
      if (numCopiesOption > 1)
      {
        attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
                                     IPPDefs.TAG_INTEGER,
                                     "copies" );
        attrs[i].addInteger( numCopiesOption );
        i++;
      }
      if ((pageLowerOption > 0) && (pageUpperOption > 0) && (!pagePrintAll))
      {
        attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
                                     IPPDefs.TAG_RANGE,
                                     "page-ranges" );
        attrs[i].addRange( pageLowerOption, pageUpperOption );
        i++;
      }
      return(attrs);
    }



    // Implementation of ActionListener interface.
    public void actionPerformed(ActionEvent e) 
    {
      Object source = e.getSource();

      //
      //  Name typed in
      //
      if (source == printAllCheckBox)
      {
        JCheckBox cb = (JCheckBox)source;
        pagePrintAll = cb.isSelected();
        pageLowerField.setEnabled(!pagePrintAll);
        pageUpperField.setEnabled(!pagePrintAll);
      }
      else if (source == pageLowerField)
      {
        String s = pageLowerField.getText();
        if (s.length() > 1)
        {
          pageLowerOption = new Integer(s).intValue();
          // if (pageLowerOption > 0)
          //  printAllCheckBox.setChecked(false);
        }
      }
      else if (source == pageUpperField)
      {
        String s = pageUpperField.getText();
        if (s.length() > 1)
        {
          pageUpperOption = new Integer(s).intValue();
          // if (pageUpperOption > 0)
          //  printAllCheckBox.setChecked(false);
        }
      }
      else if (source == orientationBox)
      {
        JComboBox cb = (JComboBox)source;
        selectedOrientation = cb.getSelectedIndex();
        orientationOption = orientationValues[selectedOrientation];
      }
      else if (source == jobSheetsBox)
      {
        JComboBox cb = (JComboBox)source;
        selectedJobSheets = cb.getSelectedIndex();
        jobSheetsOption = jobSheetsNames[selectedJobSheets];
      }
      else if (source == numCopiesField)
      {
        String s = numCopiesField.getText();
        if (s.length() >= 1)
        {
          numCopiesOption = new Integer(s).intValue();
        }
      }
      else if (source == fileTextField) 
      {
        String s = fileTextField.getText();
        if (s.length() > 1)
        {
          fileName = s;
        } 
      }
      else if (source == printButton)
      {
        if (fileName.length() > 1)
        {
          job = printFile( fileName );
          if (job != null)
          {
            fileName = "";
            fileTextField.setText("");
            JOptionPane.showMessageDialog(mainPanel, 
                        "Job " + printer.getPrinterName() + "-" +
                        new Integer(job.job_id).toString() + 
                        " queued.");
          }
        }
      }
    }





    public class MyTextListener implements FocusListener
    {

      public void focusGained(FocusEvent e)
      {
      }

      public void focusLost(FocusEvent e)
      {
        JTextField txtField = (JTextField)e.getSource();
        if (txtField == numCopiesField)
        {
          String s = numCopiesField.getText();
          if (s.length() >= 1)
          {
            numCopiesOption = new Integer(s).intValue();
          }
        }
        else if (txtField == pageLowerField)
        {
          String s = pageLowerField.getText();
          if (s.length() >= 1)
          {
            pageLowerOption = new Integer(s).intValue();
          }
        }
        else if (txtField == pageUpperField)
        {
          String s = pageUpperField.getText();
          if (s.length() >= 1)
          {
            pageUpperOption = new Integer(s).intValue();
          }
        }
      }
    }

}