West Farm
吾本布衣,生于川北,躬耕于代码的田地上。
posts - 16,  comments - 15,  trackbacks - 0
天天冒出一堆新的东西!让人不知所措。

举个例子:jquery - angular - react - vue

这语言真的有毒。

如今的硬件,恐怕在浏览器中实现强类型语言JAVA来作为脚本语言都不比JS弱吧,哎真替applet感到冤枉。

老夫一直就觉得,浏览器一直是一个很奇葩的东西,HTML+CSS+JS本身,难道不能发明一种语言通过HTTP传输,然后直接调用OS级别的UI来渲染不就完了吗?

网页能实现的界面,我不信用操作系统的UI组件做不出来。
posted @ 2018-04-01 00:19 West Farmer 阅读(164) | 评论 (0)编辑 收藏
原文地址:https://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html

正则表达式中,\A是指一个字符串的开头,可能大家用惯了^,而忽略了这个\A, 但是^其实是指一行的开始,而不管一个字符串里面包含多少行,\A都只匹配第一行的开头。
那么我们可以用
\A做点什么呢?,看下面这个例子:
static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}
相信大家都看懂了,用\A作为分隔符,那么得到的当然就只有一个token,那就是整个字符串了。再配合Scanner,就轻易的将一个输入流转换为一个字符串了。
注意当需要做编码转换时,Scanner是有支持编码参数的构造方法的。
posted @ 2013-11-28 09:38 West Farmer 阅读(1007) | 评论 (0)编辑 收藏
     摘要: Gson is library created by google guys, it is used for java bean to json serialization/deserialization. Gson can serialize any java bean, collection, map to json, and don't need to care about the...  阅读全文
posted @ 2013-11-22 19:22 West Farmer 阅读(5315) | 评论 (3)编辑 收藏
If we observe a property of SWT controls, and bind it to another observable value, you must take care of those method calls which will change the property indirectly.
for example, we have a Combo whose "text" property is bound to a bean's "name" property like this:
IObservableValue nameObservable = BeansObservables.observeDetailValue(obserabedDriverProfile, "name", String.class);

ISWTObservableValue nameComboObservable = SWTObservables.observeText(driverClassCombo);

bindingContext.bindValue(nameComboObservable , nameObservable, new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE), null);

The variable "obserabedDriverProfile" is a observed selection in a ListViewer, it's in a master-detail scenario. If we add some code like this:
obserabedDriverProfile.addValueChangeListener(new IValueChangeListener(){

            @Override
            public void handleValueChange(ValueChangeEvent event) {
                DriverProfile dp = (DriverProfile)event.diff.getNewValue();
                driverClassCombo.removeAll();
                driverClassCombo.add(dp.getName();
                driverClassCombo.setText(driverClassCombo.getItem(0));
            }
            
        });
every time you change the selection in the ListViewer,  the selected bean's "name" property will be set to a empty string. Why?  The removeAll method of Combo will clear it's text, and combo's "text" property is bound to selected bean's "name" property. So, the right way is:
obserabedDriverProfile.addValueChangeListener(new IValueChangeListener(){

            @Override
            public void handleValueChange(ValueChangeEvent event) {
                DriverProfile dp = (DriverProfile)event.diff.getNewValue();
                String name = dp.getName;
                driverClassCombo.removeAll();
                driverClassCombo.add(name);
                driverClassCombo.setText(driverClassCombo.getItem(0));
            }
            
        });
posted @ 2013-04-14 15:24 West Farmer 阅读(248) | 评论 (0)编辑 收藏
本文用英文写的,主要是考虑到本文分享的内容即使在google上也搜索不到(至少我是没有搜索到)。

My English is at a very low level, don't care about this fact, just focus on the idea shared here.

The idea comes from the source code of ConfigurationElement which is located in package org.eclipse.core.internal.registry, If you read through the source code, you can also get it. But I found there is no documentation about this topic, so I wrote this.

Sometimes we need to contribute java class to a extension point. And we can use ConfigurationElement#
createExecutableExtension(String attributeName) to create an instance of it, if such a class is just a normal class, eclipse will  call class#newInstance(). But there is obvious restriction for using this approach, can't pass parameter in to create instance for example.

There are three different way how eclipse create instance of your class.

  1. normal, call class#newInstance()
  2. if your calss implements IExecutableExtension interface, IExecutableExtension#setInitializationData(IConfigurationElement config, String propertyName, Object data) will be called on the instance returned by class#newInstance()
  3. if your calss implements IExecutableExtensionFactory interface, IExecutableExtensionFactory#create() will be called

When you use the second method or the third method, you can pass in parameters, check the source code of ConfigurationElement, you will know how to do that. :D forgive me, I'm lazy. 
posted @ 2012-04-15 16:33 West Farmer 阅读(324) | 评论 (0)编辑 收藏
SWT的Table不够强大,而且似乎有严重的性能问题。(貌似是调用OS的实现,但是在win7上面跑却非常慢,奇特!本地的性能不如虚拟机上跑的Swing)

好在SWT中可以嵌入AWT。

反正本人以前没有过多Swing的经验,但是在试玩了JTable之后发现确实很强大。

对于有兴趣的读者可以试一下,本文将分享如何使得JTable与JFace Data Binding Framework(下文中简称JDBF)一起协同工作。

通常像Table和List这种UI组件,展现的都是一个对象集合。JDBF 则为我们处理对象集合和UI界面的同步的问题。以List为例子,在Java中有java.util.List,而JFDB则提供了相应的
ObservableList类,这个类Wrap一个java.util.List,当你对其进行增删改时,与其绑定的UI组件会自动得到同步。但是ObservableList 的实现有个很大的问题就是对其的访问只限于其所属的Realm,这个Realm说白了就是SWT的UI线程,而当我们在SWT中嵌入AWT时,其中的AWT界面是跑在其自身的线程里面的。所以想要让JTable与JFace Data Binding Framework(下文中简称JDBF)一起协同工作还要解决多线程的问题。废话不多说了,直接上菜:

 public class DOTableModel extends AbstractTableModel  {
    /**
     * the ObservableList instance  to be shared with
     
*/
    private ObservableList list;
    
    private volatile  Integer  rowCount = null;
    
    private volatile DOModel object;;
    
    private  final Object lock = new Object();

    private static final long serialVersionUID = -8377145381412656796L;
    
    public DOTableModel(ObservableList list){
        this.list = list;
        this.list.addListChangeListener(new IListChangeListener(){
            @Override
            public void handleListChange(ListChangeEvent event) {
                for(ListDiffEntry de : event.diff.getDifferences()){
                    if(de.isAddition()){
                        DOTableModel.this.fireTableRowsInserted(de.getPosition(), de.getPosition());
                    }else{
                        DOTableModel.this.fireTableRowsDeleted(de.getPosition(), de.getPosition());
                    }
                }
            }
        });
    }

    @Override
    public int getRowCount() {
        list.getRealm().exec(new Runnable(){
            @Override
            public  void run() {
                rowCount = list.size();
                synchronized (lock) {
                    lock.notify();
                 }
            }
        });
        synchronized (lock) {
            while(rowCount == null){
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
         }
        return rowCount;
    }

    @Override
    public int getColumnCount() {
        return 11;
    }
    
    private void getObjectFromSWTRealm(final int rowIndex){
        object =  null;
        list.getRealm().exec(new Runnable(){
            @Override
            public synchronized void run() {
                object = (DOModel) list.get(rowIndex);
                synchronized (lock) {
                    lock.notify();
                 }
            }
        });
        synchronized (lock) {
            while(object == null){
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
         }
    }

    @Override
    public Object getValueAt(final int rowIndex, int columnIndex) {
        getObjectFromSWTRealm(rowIndex);
        ...
    }

    @Override
    public void setValueAt(Object oValue, final int rowIndex, int columnIndex) {
        getObjectFromSWTRealm(rowIndex); 
        ...
    }

    @Override
    public String getColumnName(int column) {
       ...
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }
    
    

}





posted @ 2012-04-05 23:46 West Farmer 阅读(694) | 评论 (2)编辑 收藏
class Java {

    Java Java;

    Java() {
        Java = Java(this);
    }

    Java(Java Java) {
        this.Java = Java;
    }

    Java Java() {
        return Java.this.Java;
    }

    Java Java(Java Java) {
        return new Java(Java);
    }

    public static void main(String[] args) {
        new Java().Java.Java().Java.Java();
    }

}

原文地址
posted @ 2012-04-05 23:25 West Farmer 阅读(577) | 评论 (0)编辑 收藏
public static void main(String[] args) throws IOException {
        File sourceFile 
= new File("c:\\java\\A.java");
        JavaCompiler compiler 
= ToolProvider.getSystemJavaCompiler();
        System.out.println(System.getProperties().getProperty(
"java.class.path"+ ";F:\\IndigoSpace\\ejp");
        compiler.run(
nullnullnull"-cp", System.getProperties().getProperty("java.class.path"+ ";F:\\IndigoSpace\\ejp", sourceFile.getPath());
        System.out.println(
new File("c:\\java\\").toURI().toURL());
        URLClassLoader loader 
= new URLClassLoader(new URL[]{new File("c:\\java\\").toURI().toURL()});
        
try {
            loader.loadClass(
"A");
        } 
catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
需要注意的是,上面的代码只有在JDK上才能运行,因为JDK里面才有javac。而且在实际应用中,你还要自己将package声明转换成文件目录,否者装载类的时候就会找不到。
posted @ 2011-10-21 17:05 West Farmer 阅读(328) | 评论 (0)编辑 收藏

<2011年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿

随笔分类

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜