hkbmwcn

          2008年1月18日 #

          關(guān)于JInternalFrame去掉Title bar的問題

          自己實(shí)在是個(gè)懶人,blog難得更新一次,更新也是一些雞毛蒜皮的小東西。不過還是希望能對(duì)其他朋友或自己將來遇到類似問題能有個(gè)解答。最新在做一個(gè)swing項(xiàng)目,客戶要求能把JInternalFrame的Title bar去掉,同時(shí)還能加回來。由于網(wǎng)上搜一下沒有找到解決辦法,只能自己研究一下并改了下JInternalFrame,先記錄如下:


          import java.awt.BorderLayout;
          import java.awt.Dimension;
          import java.awt.Font;
          import java.awt.Rectangle;
          import java.awt.event.ComponentEvent;
          import java.awt.peer.ComponentPeer;
          import java.beans.PropertyVetoException;

          import javax.swing.ActionMap;
          import javax.swing.BorderFactory;
          import javax.swing.JComponent;
          import javax.swing.JDesktopPane;
          import javax.swing.JInternalFrame;
          import javax.swing.UIManager;
          import javax.swing.plaf.InternalFrameUI;
          import javax.swing.plaf.basic.BasicInternalFrameUI;

          public class MCOCInternalFrame extends JInternalFrame {
              
              //private String lookAndFeel = null;
              BasicInternalFrameUI orgUi = null;
              BasicInternalFrameUI newUi = null;
              JComponent northPanel = null;
              private boolean isHidden = false;

              
              public MCOCInternalFrame() {
                  super();

                  northPanel = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).getNorthPane();
                  orgUi = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI());
                  newUi = new BasicInternalFrameUI(this);        
              }
              
              
              public void showNorthPanel() {

                  
                  this.setUI(orgUi);
                  this.putClientProperty("JInternalFrame.isPalette", Boolean.FALSE);
                  isHidden = false;

              }
              
              public void hideNorthPanel() {
                  this.setUI(newUi);
                  ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
                  this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
                  isHidden = true;
                  
              }
              
               public void updateUI() {
                  
                      super.updateUI();
                      if (isHidden) {
                          hideNorthPanel();
                      }
               }


          }

          創(chuàng)建該InternalFrame對(duì)象后,通過showNorthPanel(), hideNorthPanel()來顯示或隱藏title bar,另外updateUI()重寫是因?yàn)榻缑姹粍?dòng)態(tài)改變lookandfeel時(shí),保證title bar上多的一小個(gè)bar出現(xiàn)。


          posted @ 2008-01-18 22:02 亙古頑石 閱讀(637) | 評(píng)論 (0)編輯 收藏

          javax mail 發(fā)送郵件及附件

          MailSender.java

          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.Properties;

          import javax.activation.DataHandler;
          import javax.activation.DataSource;
          import javax.activation.FileDataSource;
          import javax.mail.MessagingException;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.AddressException;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeBodyPart;
          import javax.mail.internet.MimeMessage;
          import javax.mail.internet.MimeMultipart;

          import org.apache.log4j.Logger;

          public class MailSender {
           public static Logger logger = Logger.getLogger(MailSender.class);
           public static boolean send(Mail mail) throws Exception {
            try {
             Properties props = new Properties();
             props.put("mail.smtp.host", "localhost");
             Session session = Session.getDefaultInstance(props, null);
             MimeMessage mimemessage = new MimeMessage(session);
             mimemessage.setFrom(new InternetAddress(mail.getFrom()));
             mimemessage.setSentDate(mail.getDate());
             // set SUBJECT
             mimemessage.setSubject(mail.getSubject());

             // set TO address
             String mailto = mail.getTo();
             String ccmailid = mail.getCcusers();
             String strResult = "";
             try {
              mimemessage.setRecipients(javax.mail.Message.RecipientType.TO,
                mailto);
             } catch (Exception exception1) {
              throw exception1;
             }

             // set message BODY
             MimeBodyPart mimebodypart = new MimeBodyPart();
             mimebodypart.setText(mail.getContent());

             // attach message BODY
             MimeMultipart mimemultipart = new MimeMultipart();
             mimemultipart.addBodyPart(mimebodypart);

             // attach FILE
             ArrayList attachedFileList = mail.getAttachedFileList();
             if (attachedFileList != null) {
              DataSource ds = null;;
              for (Iterator e = attachedFileList.iterator(); e.hasNext();) {
               ds = (DataSource) e.next();
               mimebodypart = new MimeBodyPart();
               try {
                mimebodypart.setDataHandler(new DataHandler(
                  ds));
               } catch (Exception exception3) {
                throw exception3;
               }
               mimebodypart.setFileName(ds.getName()); // set FILENAME
               mimemultipart.addBodyPart(mimebodypart);
              }
             }// end if
             mimemessage.setContent(mimemultipart);
             // set CC MAIL and SEND the mail
             if (!mailto.equals("")) {
              // set CC MAIL
              if (ccmailid != null && (!ccmailid.equals("")))
               mimemessage.setRecipients(
                 javax.mail.Message.RecipientType.CC, ccmailid);
              try {
               // send MAIL
               Transport.send(mimemessage);
               logger.info(mailto + " Sent Successfully..........");
              } catch (Exception exception4) {
               throw exception4;
              }
             } else {
              logger.info(mailto + " Mail operation Failed..........");
             }
            } catch (Exception e) {
             throw e;
            }
            return true;
           }

          }

          Mail.java
          import java.util.ArrayList;
          import java.util.Date;
          import java.util.StringTokenizer;

          public class Mail {
           
           private String from = null;
           private String to = null;
           private String subject = null;
           private String content = null;
           private String ccusers = null;
           private ArrayList attachedFileList = null;
           private Date date = null;

           public Mail() {
            // TODO Auto-generated constructor stub
           }

           public ArrayList getAttachedFileList() {
            return attachedFileList;
           }

           public void setAttachedFileList(ArrayList attachedFileList) {
            this.attachedFileList = attachedFileList;
           }

           


           public String getContent() {
            return content;
           }

           public void setContent(String content) {
            this.content = content;
           }

           public String getFrom() {
            return from;
           }

           public void setFrom(String from) {
            this.from = from;
           }

           public String getSubject() {
            return subject;
           }

           public void setSubject(String subject) {
            this.subject = subject;
           }

           public String getTo() {
            return to;
           }

           public void setTo(String to) {
            this.to = to;
           }

           public Date getDate() {
            return date;
           }

           public void setDate(Date date) {
            this.date = date;
           }

           public String getCcusers() {
            return ccusers;
           }

           public void setCcusers(String ccusers) {
            this.ccusers = ccusers;
           }

          }




          posted @ 2008-01-18 21:50 亙古頑石 閱讀(2071) | 評(píng)論 (1)編輯 收藏

          2007年7月30日 #

          密碼的加解密

          很長(zhǎng)時(shí)間沒有更新自己的blog了,最近忙一個(gè)基于alfreco的項(xiàng)目開發(fā),趕快把用到的一些東西記錄一下,誰讓自己記性不好呢。這里是password的加解密:
          PasswordUtil.java

          import java.io.IOException;
          import java.security.MessageDigest;
          import java.security.NoSuchAlgorithmException;
          import java.security.Security;

          import javax.crypto.Cipher;
          import javax.crypto.KeyGenerator;
          import javax.crypto.SecretKey;

          import sun.misc.BASE64Decoder;
          import sun.misc.BASE64Encoder;

          public class PasswordUtil {
           public final static int ITERATION_NUMBER = 1000;
           public static final String algorithm = "Blowfish";// we can use DES,DESede,Blowfish
           
           /**
            *
            * @param password
            * @return
            * @throws Exception
            */
           public static PasswordBean encry(String password) throws Exception {
            Security.addProvider(new com.sun.crypto.provider.SunJCE());

            try {
             KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
             SecretKey deskey = keygen.generateKey();
             Cipher c1 = Cipher.getInstance(algorithm);
             c1.init(Cipher.ENCRYPT_MODE, deskey);
             byte[] cipherByte = c1.doFinal(password.getBytes());
             String saltKey = PasswordUtil.byteToBase64(deskey.getEncoded());
             String pass = PasswordUtil.byteToBase64(cipherByte);
             return new PasswordBean(saltKey, pass);
             //System.out.println("After encry:" + key + "====" + password);
            } catch (Exception e) {
             throw e;
            }
           }
           
           /**
            *
            * @param bean
            * @return
            * @throws Exception
            */
           public static String decry(PasswordBean bean) throws Exception {
            return decry(bean.getSaltKey(), bean.getPassword());
           }
           
           /**
            *
            * @param saltKey
            * @param pass
            * @return
            * @throws Exception
            */
           public static String decry(String saltKey, String pass) throws Exception {
            //Security.addProvider(new com.sun.crypto.provider.SunJCE());
            try {
             Security.addProvider(new com.sun.crypto.provider.SunJCE());
             byte[] keyser = PasswordUtil.base64ToByte(saltKey);
             javax.crypto.spec.SecretKeySpec destmp = new javax.crypto.spec.SecretKeySpec(keyser, algorithm);
             SecretKey mydeskey = destmp;

             Cipher c1 = Cipher.getInstance(algorithm);
             c1.init(Cipher.DECRYPT_MODE, mydeskey);
             byte[] clearByte = c1.doFinal(PasswordUtil.base64ToByte(pass));
             return new String(clearByte);
            } catch (Exception e) {
             //e.printStackTrace();
             System.out.println("saltKey:" + saltKey + "   pass:" + pass) ;
             throw e;
            }
           }
           
           
           
           /**
            * From a password, a number of iterations and a salt, returns the
            * corresponding digest
            *
            * @param iterationNb
            *            int The number of iterations of the algorithm
            * @param password
            *            String The password to encrypt
            * @param salt
            *            byte[] The salt
            * @return byte[] The digested password
            * @throws NoSuchAlgorithmException
            *             If the algorithm doesn't exist
            */
           public static byte[] getHash(int iterationNb, String password, byte[] salt)
             throws NoSuchAlgorithmException {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            digest.update(salt);
            byte[] input = digest.digest(password.getBytes());
            for (int i = 0; i < iterationNb; i++) {
             digest.reset();
             input = digest.digest(input);
            }
            return input;
           }

           /**
            * From a base 64 representation, returns the corresponding byte[]
            *
            * @param data
            *            String The base64 representation
            * @return byte[]
            * @throws IOException
            */
           public static byte[] base64ToByte(String data) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            return decoder.decodeBuffer(data);
           }

           /**
            * From a byte[] returns a base 64 representation
            *
            * @param data
            *            byte[]
            * @return String
            * @throws IOException
            */
           public static String byteToBase64(byte[] data) {
            BASE64Encoder endecoder = new BASE64Encoder();
            return endecoder.encode(data);
           }
          }

          PasswordBean.java

          public class PasswordBean {
           
           private String saltKey = null;
           private String password = null;

           public PasswordBean(String key, String pass) {
            this.saltKey = key;
            this.password = pass;
           }
           
           public String getPassword() {
            return password;
           }
           public void setPassword(String password) {
            this.password = password;
           }
           public String getSaltKey() {
            return saltKey;
           }
           public void setSaltKey(String saltKey) {
            this.saltKey = saltKey;
           }
           
          }


          使用的時(shí)候可以是:
          String password = PasswordUtil.decry(salt, encodePassword);
          PasswordBean passwordBean = PasswordUtil.encry(password);

          posted @ 2007-07-30 10:42 亙古頑石 閱讀(355) | 評(píng)論 (0)編輯 收藏

          2007年3月28日 #

          System.getProperty()參數(shù)大全

          常用到System.getProperty(), 而參數(shù)老不記得,這里貼一下,省得下次麻煩.

          java.version

          Java Runtime Environment version
          java.vendorJava Runtime Environment vendor
          java.vendor.urlJava vendor URL
          java.homeJava installation directory
          java.vm.specification.versionJava Virtual Machine specification version
          java.vm.specification.vendorJava Virtual Machine specification vendor
          java.vm.specification.nameJava Virtual Machine specification name
          java.vm.versionJava Virtual Machine implementation version
          java.vm.vendorJava Virtual Machine implementation vendor
          java.vm.nameJava Virtual Machine implementation name
          java.specification.versionJava Runtime Environment specification version
          java.specification.vendorJava Runtime Environment specification vendor
          java.specification.nameJava Runtime Environment specification name
          java.class.versionJava class format version number
          java.class.pathJava class path
          java.library.pathList of paths to search when loading libraries
          java.io.tmpdirDefault temp file path
          java.compilerName of JIT compiler to use
          java.ext.dirsPath of extension directory or directories
          os.nameOperating system name
          os.archOperating system architecture
          os.versionOperating system version
          file.separatorFile separator ("/" on UNIX)
          path.separatorPath separator (":" on UNIX)
          line.separatorLine separator ("\n" on UNIX)
          user.nameUser's account name
          user.homeUser's home directory
          user.dirUser's current working directory

          posted @ 2007-03-28 20:12 亙古頑石 閱讀(313) | 評(píng)論 (0)編輯 收藏

          2006年9月10日 #

          一個(gè)時(shí)代的結(jié)束!

          說實(shí)話,一直不想在這里寫一些不是關(guān)于技術(shù)方面的東西.但是今天,我實(shí)在想寫一點(diǎn)什么:今天舒馬赫宣布退役了!
          令我自己也想不通,為什么是今天.而不是米卡.哈基寧退出F1的那一天,也不是Montoya退出的那一天.因?yàn)槲乙恢笔撬麄儍蓚€(gè)的車迷!自98年開始看F1以來,一直是一個(gè)不向著舒馬赫的車迷(倒和維綸紐夫差不多,不過我可沒那本事和舒米對(duì)著干!呵呵).
          剛開始,一直喜歡哈基寧,因?yàn)橹挥兴拍軓氖骜R赫手中奪取年度冠軍,而且不止一次!但是隨著Ferrari的強(qiáng)盛一時(shí),麥克拉倫沒落了,我心中的英雄也暗然退去.然后,激情四射的哥倫比亞人Montoya來了,他也一度給舒米制造了不少麻煩,而且看他開車我也會(huì)充滿激情.而且最喜歡他跑今天的蒙扎賽道.在Williams時(shí)就在蒙扎取的過冠軍,去年也是,可是今年呢?昔日的蒙扎英雄何在?
          哈基寧的退出令人傷心,長(zhǎng)期的壓抑作為他的車迷也可以感受到他的心情.Montoya的退出實(shí)在是太突然了,突然的我都不會(huì)說話了.
          今天舒米也退了,作為車迷還是很傷心,畢竟我的偶像們都曾是他的手下敗將!他的技術(shù)卻實(shí)高人一等,雖然不少時(shí)候他也做法卑鄙,畢竟是人嘛!也可以理解.
          所以今天寫點(diǎn)胡話,紀(jì)念一下舒米,同時(shí)也為追憶(當(dāng)然不恰當(dāng)?shù)恼f法拉)一下我的兩位偶像!

          posted @ 2006-09-10 23:27 亙古頑石 閱讀(195) | 評(píng)論 (0)編輯 收藏

          2006年9月6日 #

          debian etch中設(shè)置postgresql 8.1

          數(shù)據(jù)庫(kù)方面一直喜歡用postgresql,最近重裝了debian.安裝的時(shí)候發(fā)現(xiàn)可以用apt-get 方法安裝postgresql了,就安裝了一個(gè).結(jié)果裝完發(fā)現(xiàn)居然沒有啟動(dòng),仔細(xì)一看初始化也沒做,debian這一點(diǎn)做的有點(diǎn)不應(yīng)該阿!!于是自己重新 做了一下.現(xiàn)在記下來,不要下次忘了.HOHO!記性不好.
          用postgres登錄,切換到目錄/usr/lib/postgresql/8.1/bin(是系統(tǒng)安裝的目錄,居然沒有設(shè)置成Path汗).運(yùn)行./initdb -D /usr/local/pgsql/data(當(dāng)然,后面的目錄也沒有,自己建先),成功運(yùn)行以后就可以啟動(dòng)了. ./postmaster -D /usr/local/pgsql/data
          不知道debian安裝默認(rèn)允許非本機(jī)用戶登錄伐,我先看看...

          posted @ 2006-09-06 16:44 亙古頑石 閱讀(392) | 評(píng)論 (0)編輯 收藏

          2006年4月20日 #

          用regular expressoions判斷url合法性

          在網(wǎng)上可以看到很多判斷判斷url是否合法的regular expressions.但是經(jīng)常是要么缺少protocol,要么缺少port的判斷,這里自己寫一個(gè):
          ?public static boolean isValidURL(String value) {
          ??Pattern pattern = Pattern.compile("(.*://)?([\\w-]+\\.)+[\\w-]+(:\\d+)?(/[^/.]*)*(/[^/]+\\.[^/\\?]+)(\\?&*([^&=]+=[^&=]*)&*(&[^&=]+=[^&=]*)*&*)");
          ??Matcher m = pattern.matcher(value);
          ??if (m.matches())
          ???return true;
          ??
          ??return false;
          ?}

          判斷url中data是否符合規(guī)則:
          &*([^&=]+=[^&=]*)&*(&[^&=]+=[^&=]*)*&*

          posted @ 2006-04-20 10:42 亙古頑石 閱讀(723) | 評(píng)論 (1)編輯 收藏

          2006年1月13日 #

          rcp 程序最小化窗口到系統(tǒng)托盤

          在RCP程序中最小化窗口到系統(tǒng)托盤并不像SWT程序那樣直接。在網(wǎng)上找了一段代碼,自己試驗(yàn)通過了,現(xiàn)在貼出來大家分享。
          在RCP中繼承WorkbenchAdvisor的子類中添加下面代碼就可以了。

           public void postStartup() {
            super.postStartup();
            final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            shell.addShellListener(new ShellAdapter() {
             public void shellIconified(ShellEvent e) {
              shell.setVisible(false);
             }
            });
            createSystemTray();

           }

           private void createSystemTray() {
            Tray tray = Display.getDefault().getSystemTray();
            TrayItem item = new TrayItem(tray, SWT.NONE);
            item.setText("JDishNetwork");
            item.setToolTipText("JDishNetwork");
            Image image = new Image(Display.getDefault(), 16, 16);
            item.setImage(image);
            this.trayManager = new TrayItemManager();
            item.addSelectionListener(this.trayManager);
            item.addListener(SWT.MenuDetect, this.trayManager);
           }

           private class TrayItemManager implements SelectionListener, Listener {
            // TODO: Convert to one class
            private final class WindowStateListener extends SelectionAdapter {
             public void widgetSelected(SelectionEvent e) {
              minimizeWindow();
             }
            }
            private final class RestoreWindowListener extends SelectionAdapter {
             public void widgetSelected(SelectionEvent e) {
              restoreWindow();
             }
            }
            private boolean minimized = false;
            private Menu menu;
            private MenuItem[] menuItems = new MenuItem[0];
            private RestoreWindowListener restoreWindowListener;
            private WindowStateListener minimizeWindowListener;

            public TrayItemManager() {
             this.menu = new Menu(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.POP_UP);
             this.restoreWindowListener = new RestoreWindowListener();
             this.minimizeWindowListener = new WindowStateListener();

            }

            protected void closeApplication() {
             PlatformUI.getWorkbench().close();
            }

            public void widgetSelected(SelectionEvent e) {
             //
            }

            public void widgetDefaultSelected(SelectionEvent e) {
             if (this.minimized) {
              restoreWindow();
             } else {
              minimizeWindow();
             }
            }

            protected void minimizeWindow() {
             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setMinimized(true);
             this.minimized = true;
            }

            protected void restoreWindow() {
             Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
             shell.open();
             shell.setMinimized(false);
             shell.forceActive();
             shell.forceFocus();
             this.minimized = false;
            }

            public void showMenu() {
             clearItems();
             MenuItem mi;
             MenuItem closeItem;
             mi = new MenuItem(this.menu, SWT.PUSH);
             closeItem = new MenuItem(this.menu, SWT.NONE);
             closeItem.setText("Close");
             closeItem.addSelectionListener(new SelectionAdapter() {
              public void widgetSelected(SelectionEvent e) {
               closeApplication();
              }
             });
             this.menuItems = new MenuItem[] { mi, closeItem };

             if (this.minimized) {
              mi.setText("Maximize");
              mi.addSelectionListener(this.restoreWindowListener);
             } else {
              mi.setText("Minimize");
              mi.addSelectionListener(this.minimizeWindowListener);
             }
             this.menu.setVisible(true);
            }

            private void clearItems() {
             for (int i = 0; i < this.menuItems.length; i++) {
              MenuItem item = this.menuItems[i];
              item.removeSelectionListener(this.minimizeWindowListener);
              item.removeSelectionListener(this.restoreWindowListener);
              this.menuItems[i].dispose();
             }
            }

            public void handleEvent(Event event) {
             showMenu();
            }

           }

          posted @ 2006-01-13 15:31 亙古頑石 閱讀(886) | 評(píng)論 (0)編輯 收藏

          2005年12月6日 #

          我的source.list

          deb http://ftp.hk.debian.org/debian/ sid main
          deb http://ftp.hk.debian.org/debian/ experimental main
          #deb http://pkg-gnome.alioth.debian.org/debian/ experimental main
          deb http://security.debian.org/ testing/updates main
          #deb http://ftp.debian.org/debian/ testing main

          posted @ 2005-12-06 13:07 亙古頑石 閱讀(322) | 評(píng)論 (0)編輯 收藏

          2005年11月30日 #

          SWT browser類修改

               摘要: 在swt提供的browser 類中間,缺少不少有用的方法。我在使用時(shí)用到兩個(gè),getText()和setUrl(String url, String post_data),分別取得當(dāng)前打開頁面的原代碼和采用post方式打開頁面?,F(xiàn)在修改Browser類如下:/**********************************************************************...  閱讀全文

          posted @ 2005-11-30 10:25 亙古頑石 閱讀(3758) | 評(píng)論 (2)編輯 收藏

          僅列出標(biāo)題  
          主站蜘蛛池模板: 革吉县| 遂川县| 栖霞市| 延庆县| 平谷区| 通许县| 东港市| 大新县| 广平县| 咸宁市| 灵武市| 建湖县| 六盘水市| 靖州| 密山市| 普陀区| 泰州市| 砀山县| 嵊泗县| 车险| 阜新| 水富县| 沙田区| 镇康县| 临高县| 北流市| 茌平县| 东乡县| 喀什市| 武隆县| 余姚市| 嘉峪关市| 南阳市| 辽宁省| 德昌县| 凌海市| 上饶市| 桐柏县| 武穴市| 墨脱县| 汉沽区|