Java tutorial
/* * Copyright 2011 Szabolcs Berecz * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package domper.config; import static com.google.common.collect.Iterables.transform; import static java.lang.String.format; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import domper.util.DataType; import domper.util.RecordItem; import domper.util.RecordItemExtractor; public class ConfigEditorActivity extends Activity { private static final String TAG = "ConfigEditorActivity"; public static void addMenuItem(final Context context, Menu menu) { final MenuItem item = menu.add("Config Editor"); item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { context.startActivity(createIntent(context)); return true; } }); } public static void updateMenuItem(Context context, Menu menu) { } public static Intent createIntent(Context context) { return new Intent(context, ConfigEditorActivity.class); } private ViewGroup editorsContainer; private ConfigStore<Serializable> configStore; private List<ConfigEditor<?>> configEditors; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.config_editor); final Button saveAndCloseButton = (Button) findViewById(R.id.button_save_and_close); saveAndCloseButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { saveAndClose(); } }); final Button resetButton = (Button) findViewById(R.id.button_reset); resetButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { reset(); } }); editorsContainer = (ViewGroup) findViewById(R.id.editors); configStore = ConfigStore.getInstance(); } @Override protected void onResume() { super.onResume(); configEditors = createConfigEditors(); editorsContainer.removeAllViews(); for (ConfigEditor<?> configEditor : configEditors) { editorsContainer.addView(configEditor.getView()); } } private void saveAndClose() { final Serializable currentConfig = configStore.getCurrentConfig(); final Map<String, RecordItem<?>> currentRecordItems = extractRecordItems(currentConfig); for (ConfigEditor<?> configEditor : configEditors) { currentRecordItems.get(configEditor.getConfigItemName()).updateField(currentConfig, configEditor.getCurrentValue()); } configStore.update(currentConfig); finish(); } private void reset() { final Serializable defaultConfig = configStore.getDefaultConfig(); final Map<String, RecordItem<?>> currentRecordItems = extractRecordItems(defaultConfig); for (ConfigEditor<?> configEditor : configEditors) { configEditor.updateGenericsHack(currentRecordItems.get(configEditor.getConfigItemName()).getValue()); } } private final Function<RecordItem<?>, String> recordItemName = new Function<RecordItem<?>, String>() { @Override public String apply(RecordItem<?> item) { return item.getName(); } }; private Map<String, RecordItem<?>> extractRecordItems(Object configObject) { return Maps.uniqueIndex(RecordItemExtractor.extract(configObject), recordItemName); } private List<ConfigEditor<?>> createConfigEditors() { final List<ConfigItem<?>> configItems = createConfigItems(configStore.getCurrentConfig(), configStore.getDefaultConfig()); final LayoutInflater inflater = LayoutInflater.from(this); List<ConfigEditor<?>> configEditors = ImmutableList .copyOf(transform(configItems, new Function<ConfigItem<?>, ConfigEditor<?>>() { @Override public ConfigEditor<?> apply(ConfigItem<?> configItem) { return createConfigEditor(inflater, configItem); } })); return configEditors; } private List<ConfigItem<?>> createConfigItems(Serializable currentConfig, Serializable defaultConfig) { final List<RecordItem<?>> currentRecordItems = RecordItemExtractor.extract(currentConfig); final Map<String, RecordItem<?>> defaultRecordItems = extractRecordItems(defaultConfig); final List<ConfigItem<?>> result = new ArrayList<ConfigItem<?>>(currentRecordItems.size()); for (RecordItem<?> item : currentRecordItems) { ConfigItem<?> configItem = createConfigItem(defaultRecordItems, item); result.add(configItem); } return result; } private ConfigItem<?> createConfigItem(final Map<String, RecordItem<?>> defaultRecordItems, RecordItem<?> item) { final RecordItem<?> defaultRecordItem = defaultRecordItems.get(item.getName()); @SuppressWarnings({ "rawtypes", "unchecked" }) ConfigItem<?> configItem = new ConfigItem(defaultRecordItem, item); return configItem; } @SuppressWarnings("unchecked") private static ConfigEditor<?> createConfigEditor(LayoutInflater inflater, ConfigItem<?> configItem) { final DataType type = configItem.type; final ConfigEditor<?> configEditor; switch (type) { case Boolean: configEditor = new BooleanConfigEditor(inflater, (ConfigItem<Boolean>) configItem); break; case Double: configEditor = new DoubleConfigEditor(inflater, (ConfigItem<Double>) configItem); break; case Integer: configEditor = new IntegerConfigEditor(inflater, (ConfigItem<Integer>) configItem); break; case Long: configEditor = new LongConfigEditor(inflater, (ConfigItem<Long>) configItem); break; case String: configEditor = new StringConfigEditor(inflater, (ConfigItem<String>) configItem); break; case Unhandled: default: Log.e(TAG, format("Unhandled config item: %s", configItem)); configEditor = new UnhandledConfigEditor(inflater, (ConfigItem<Object>) configItem); break; } return configEditor; } }