java jdk replace native class-file - class

I have ImageView.class how to get the program to use it instead of the native javax.swing.text.html.ImageView?
javax.swing.text.html.ImageView map = new javax.swing.text.html.ImageView(); //does not work
I was told that it is necessary to use ClassFileTransformer and ClassLoader, but I can not find a working examples

I think that what you really want to do is to...
Extend HTMLEditorKit and override getViewFactory()
Have it return a class that extends HTMLEditorKit.HTMLFactory
In that class, override create() to return your custom view for <img> and super.create() otherwise
Like this:
class MyImageKit extends HTMLEditorKit {
private static final MyImageFactory myFactory = new MyImageFactory();
public ViewFactory getViewFactory() {
return myFactory;
}
static class MyImageFactory extends HTMLFactory {
public View create(Element elem) {
Object type = elem.getAttributes()
.getAttribute(StyleConstants.NameAttribute);
if(type == HTML.Tag.IMG) {
return new MyImageView(elem);
} else {
return super.create(elem);
}
}
}
}
class MyImageView extends ImageView {
MyImageView(Element elem) {
super(elem);
}
protected void setPropertiesFromAttributes() {
super.setPropertiesFromAttributes();
try {
ImageView.class.getDeclaredField("vAlign").set(this, new Float(0.75f));
} catch(Exception e) {
e.printStackTrace();
}
}
}

Related

How to override a Dart method on instantiation? [duplicate]

Is there way to overriding method in Dart like JAVA, for example:
public class A {
public void handleLoad() {
}
}
And when overriding:
A a = new A() {
#Override
public void handleLoad() {
// do some code
}
};
No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.
No but it much less useful in Dart because you can just reassign function:
typedef void PrintMsg(msg);
class Printer {
PrintMsg foo = (m) => print(m);
}
main() {
Printer p = new Printer()
..foo('Hello') // Hello
..foo = ((String msg) => print(msg.toUpperCase()))
..foo('Hello'); //HELLO
}
However you will need some extra boilerplate to access instance.
Use type Function:
class A {
final Function h
A(this.h);
void handleLoad(String loadResult) { h(loadResult); }
}
Or
class A {
final Function handleLoad;
A(this.handleLoad);
}
A a = new A((String loadResult){
//do smth.
});

interface does not give me initalize method

I am missing method from interface
value of annotiations is null.
Already tried hardcoded, it works, but I need to be customizable.
package com.luv2code.springdemo.mvc.validation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CourseCodeConstraintValidator implements ConstraintValidator {
private String coursePrefix;
public void initalize(CourseCode theCourseCode) {
coursePrefix = theCourseCode.value();
}
#Override
public boolean isValid(String theCode, ConstraintValidatorContext theConstraintValidatorContext) {
/*if (coursePrefix==null)
coursePrefix = "LUV";*/
System.out.println(theConstraintValidatorContext.getDefaultConstraintMessageTemplate());
boolean result;
if (theCode!=null) {
System.out.println(coursePrefix);
result = theCode.startsWith(coursePrefix);
} else {
result = true;
}
return result;
}
}
Expecting possible #Override method initialize
initalize => initialize
just one letter missing

How can I retrieve a value from IsolatedStorage in other class?

I have two classes. First is using for store boolean value from ToggleSwitchButton by using IsolatedStorage.
Like this...
private void tglSwitch_Checked(object sender, RoutedEventArgs e)
{
System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["EnableLocation"] = true;
}
private void tglSwitch_Unchecked(object sender, RoutedEventArgs e)
{
System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["EnableLocation"] = false;
}
The second class will use the boolean value from the first class to do something.
Like this...
if(booleanValFromFirst){
//Do something
}
else{
//Do something
}
Thanks.
Is this, what you want?
if ((bool)System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["EnableLocation"] == true)
P.S. I would recommend for you to create a single class for all values, stored in Application Settings and work with it.
Like this:
public static class SettingsManager
{
private static IsolatedStorageSettings appSettings;
public static IsolatedStorageSettings AppSettings
{
get { return SettingsManager.appSettings; }
set { SettingsManager.appSettings = value; }
}
public static void LoadSettings()
{
// Constructor
if (appSettings == null)
appSettings = IsolatedStorageSettings.ApplicationSettings;
// Generate Keys if not created
if (!appSettings.Contains(Constants.SomeKey))
appSettings[Constants.SomeKey] = "Some Default value";
// generate other keys
}
}
Then you can work with that class instance
Initialize it at your startup class as SettingsManager.LoadSettings();
an then in any class just call for it:
if ((bool)SettingsManager.AppSettings[Constants.SomeBoolKey])
doSomething();

Injecting Subsonic SimpleRepository class to controller

I'm tryingot get started with IoC, I have an MVC project in which is use subsonic, I'm trying to inject subsonic simplerepository to my controllers but I'm getting this error:
StructureMap Exception Code: 205
Missing requested Instance property "connectionStringName" for InstanceKey "60b735fb-0a7f-4eb4-be04-635f6f32233d"
Here is my registry class:
public class RepositoryRegistry : Registry
{
protected override void configure()
{
ForRequestedType<IRepository>().TheDefault.Is.OfConcreteType(typeof(SimpleRepository));
}
}
And here is my controller factory:
public class StoreControllerFactory: DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
IController result = null;
if (controllerType!=null)
{
result = ObjectFactory.GetInstance(controllerType) as Controller;
}
return result;
}
}
And this is how I configure StructureMap:
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ObjectFactory.Initialize(x=>
{
x.AddRegistry(new RepositoryRegistry());
});
ControllerBuilder.Current.SetControllerFactory(new StoreControllerFactory());
var sparkSettings = new SparkSettings().SetDebug(true).AddNamespace("System.Web.Mvc.Html");
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new SparkViewFactory(sparkSettings));
}
Any help would be appreciated! Thanks!
The problem is that StructureMap was using the constructor with the most parameters, I fixed with this:
ObjectFactory.Configure(
x => { x.SelectConstructor<SimpleRepository>(() => new SimpleRepository()); });

StructureMap InstanceInterceptor not being called

I want to intercept the creation of an instance in SM and I'm trying the following but it's not calling the InstanceInterceptor implementation, does anyone know why?
ForRequestedType<IPublishResources>()
.TheDefault
.Is
.OfConcreteType<PublisherService>()
.InterceptWith(new PublisherServiceInterceptor());
The test code uses the ObjectFactory to create instances, and is shown below:
// Given we have a configure object factory in StructureMap...
ObjectFactory.Configure(x => x.AddRegistry(new StructureMapServiceRegistry()));
// When we request a publisher service...
var publisher = ObjectFactory.GetInstance<IPublishResources>();
Cheers
AWC
I could not reproduce your problem in release 2.5.4. Here is my code.
public interface IPublishResources {}
class PublishResources : IPublishResources {}
public class LoggingInterceptor : InstanceInterceptor
{
//this interceptor is a silly example of one
public object Process(object target, IContext context)
{
Console.WriteLine("Interceptor Called");
return context.GetInstance<PublishResources>();
}
}
public class MyRegistry : Registry
{
public MyRegistry()
{
For<IPublishResources>()
.Use<PublishResources>()
.InterceptWith(new LoggingInterceptor());
}
}
[TestFixture]
public class Structuremap_interception_configuraiton
{
[Test]
public void connecting_implementations()
{
var container = new Container(cfg =>
{
cfg.AddRegistry<MyRegistry>();
});
container.GetInstance<IPublishResources>();
}
}
A question. Do you really need to use an Interceptor here? If you only need to define a factory you can do somethign like this.
public interface IPublishResourcesFactory
{
IPublishResources Create();
}
public class MyRegistry : Registry
{
public MyRegistry()
{
For<IPublishResources>().Use(c =>
{
return c.GetInstance<IPublishResourcesFactory>().Create();
});
//or
For<IPublishResources>().Use(c =>
{
//other object building code.
return new PublishResources();
});
}
}